Run Timer

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pepsirice
Posts: 4
Joined: 25 Mar 2024, 21:28

Run Timer

Post by pepsirice » 25 Mar 2024, 21:44

I'm having the most difficult time trying to create a script run timer i've gutted it out of my main script just to test it and keep altering it to get it just right however it keeps either adding 60 behind the seconds, or it keeps minutes frozen at zero (haven't even tested hours yet)

Code: Select all

#Persistent  
startTime := A_TickCount 

Gui, +AlwaysOnTop
Gui, Add, Text, x20 y20 w200 h50 vRunTime, 00:00:00
Gui, Show, w240 h120, Run Time Timer 


SetTimer, UpdateRunTime, 1000
Return

UpdateRunTime:
    elapsedTime := (A_TickCount - startTime) // 1000 
    hours := elapsedTime // 3600
    minutes := Mod(elapsedTime, // 60) 
    seconds := Mod(elapsedTime, 60)
    runTime := Format("{:02d}:{:02d}:{:02d}", hours, minutes, seconds)
    GuiControl,, RunTime, %runTime%
Return

Esc::
GuiClose:
    ExitApp
Return
This is my current code, but the

Code: Select all

minutes := Mod(elapsedTime, // 60)
resets minutes to 0 every time, when I remove that and set it as

Code: Select all

minutes := elapsedTime //60 mod 60
upon the first second it shows 60 minutes (when a minute finally rolls over it'll be 160 minutes (the 60 remains 60 no matter what) which i fixed that eror occuring in seconds by setting it

Code: Select all

seconds := Mod(elapsedTime, 60)
What am I doing wrong with this? I've searched multiple times and have followed other examples, read the format documentation and am still struggling with this.

pepsirice
Posts: 4
Joined: 25 Mar 2024, 21:28

Re: Run Timer

Post by pepsirice » 25 Mar 2024, 23:32

Not entirely sure how to edit, my apologies for double post. While waiting for this to be approved I have solved my own issue... the following is what I fixed

Code: Select all

;start up code above
global startTime := A_TickCount
SetTimer, UpdateRunTime, 1000 

;gui scripts and whatnot

UpdateRunTime:
    global startTime
    elapsedTime := (A_TickCount - startTime) // 1000 
    hours := elapsedTime // 3600
    minutes := elapsedTime // 60
    seconds := Mod(elapsedTime, 60)
    runTime := Format("{:02d}:{:02d}:{:02d}", hours, minutes, seconds)
    GuiControl,, RunTime, %runTime%
Return

GuiClose:
ExitApp

autoexec
Posts: 24
Joined: 20 Feb 2023, 22:38

Re: Run Timer

Post by autoexec » 26 Mar 2024, 00:56

Hi, it should be..

Code: Select all

minutes := Mod(elapsedTime // 60, 60)

Rohwedder
Posts: 7704
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Run Timer

Post by Rohwedder » 26 Mar 2024, 01:19

Hallo,
try EnvAdd:

Code: Select all

#Persistent  
startTime := A_TickCount

Gui, +AlwaysOnTop
Gui, Add, Text, x20 y20 w200 h50 vRunTime, 00:00:00
Gui, Show, w240 h120, Run Time Timer 

SetTimer, UpdateRunTime, 1000
Return

UpdateRunTime:
	RunTime = 20240101 ; any date you like
	RunTime += (A_TickCount - startTime) // 1000, Seconds ; EnvAdd
	FormatTime, RunTime,% RunTime, HH:mm:ss
    GuiControl,, RunTime,% RunTime
Return

Esc::
GuiClose:
    ExitApp
Return
or Format_Msec():

Code: Select all

#Persistent  
startTime := A_TickCount

Gui, +AlwaysOnTop
Gui, Add, Text, x20 y20 w200 h50 vRunTime, 00:00:00
Gui, Show, w240 h120, Run Time Timer 

SetTimer, UpdateRunTime, 1000
Return

UpdateRunTime:
    GuiControl,, RunTime,% Format_Msec(A_TickCount-startTime, "hh':'mm':'ss")
Return

Esc::
GuiClose:
    ExitApp
Return

Format_Msec(ms, Format := "d' days 'hh':'mm':'ss")
{ ; Xtra: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=45476
    VarSetCapacity(T, 0x100)
    DllCall("GetDurationFormat", "Uint", 0x800, "Uint", 0, "Ptr", 0
	, "Int64", ms*10000, "WStr", Format, "WStr", T, "Int", 0x100)
    return T
}

Post Reply

Return to “Ask for Help (v1)”