Sign in to follow this  
Followers 0
logcomptechs

Script Timer?

9 posts in this topic

I would like to know how to create a timer that will keep track of the time that the script has been running for, in hours, minutes, and seconds. Basically just start from zero and keep counting up in hours, minutes, seconds, until the script is turned off.

Thanks for the Help!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Look up TimerInit() and TimerDiff() in the readme/help file.

That'll get you started.

/edit

Basic exp:

$begin = TimerInit()


While 1
    $dif = TimerDiff($begin)
    $secs = $dif/1000
    $mins = $secs/60
    $hours = $mins/60
    
    Tooltip("hours " & Int($hours) & " mins "  & Int($mins) & " secs " & Int($secs),50,50)

wend

O/c this doesn't work right. The secs/mins wont roll over at 2 mins it'll be 2mins 120 secs and at 2 hours it'll be 2 hours 120 mins xxxxx secs and so on. It won't take much work to fix/recode this but you should do it if you wana learn.

Edited by Orgins

I'm a newbie.Sorry if I don't reposed to your replays very fast.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I was already looking at that command, made something similar, but it was flickering and like you said time wasn't rolling over correctly. Thanks for your help, hopefully I can figure out a solid solution.

This is my script

$begin = TimerInit()

While 1
    $mil = TimerDiff($begin)
    $secs = TimerDiff($begin) * 0.001
    $mins = TimerDiff($begin) * 0.000016667
    $hours = TimerDiff($begin) * 0.000000278
    Tooltip("Hours" & @CRLF & Int($hours) & @CRLF & "Minutes"  & @CRLF & Int($mins) & @CRLF & "Seconds" & @CRLF & Int($secs),50,50)
Wend

It flickers and I am not sure how to take care of that.

Edited by logcomptechs

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is an example I just wrote for you, It is expanded more than it needs to be to make it easier for you. It also includes a flicker fix.

;;Create the GUI window
GUICreate("Timer",200,25)

;---------------------------- Timer GUI ------------------------------
;Label for the time
GUICtrlCreateLabel("Time Since Script has started:",5,5)
;Place to display the time
$TimerTime = GUICtrlCreateLabel("",150,5,100,25)
;Create the timer
$Timer = TimerInit()

;Time globals
$CurSec = 0
$CurMin = 0
$CurHr  = 0

;Start showing the window
GUISetState(@SW_SHOW)

;---------------------------- Main Loop ------------------------------
While Not(GUIGetMsg()==-3)
    Sleep(2)
    ;Update the Timer time
    If Not(GUICtrlRead($TimerTime)==TimerGetStringTime($Timer)) Then
        GUICtrlSetData($TimerTime,TimerGetStringTime($Timer))
    EndIf
WEnd

;--------------------- Timer Support Functions ----------------------
;Note: All of these functions could be condensed into the TimerGetStringTime function, but they are expanded for your benefit
Func TimerGetStringTime($hTimer)
    ;Fix timer flicker
    $Sec = TimerGetSec($hTimer)
    $Min = TimerGetMin($hTimer)
    $Hr  = TimerGetHrs($hTimer)
    If $CurSec<$Sec Or $CurSec-$Sec >50 Then $CurSec = $Sec
    If $CurMin<$Min Or $CurMin-$Min >50 Then $CurMin = $Min
    If $CurHr <$Hr  Or $CurHr -$Hr  >50 Then $CurHr  = $Hr
    ;Return the time
    Return String($CurHr)&":"&String($CurMin)&":"&String($CurSec)
EndFunc
Func TimerGetSec($hTimer)
    $Time = TimerDiff($Timer)   ;Get the time from the timer
    $Time = Int($Time/1000)     ;Convert milliseconds to secconds
    $Time = Mod($Time,60)       ;Remove all secconds past 60
    Return $Time                ;Return the Value
EndFunc
Func TimerGetMin($hTimer)
    $Time = TimerDiff($Timer)   ;Get the time from the timer
    $Time = Int($Time/60000)    ;Convert milliseconds to minutes
    $Time = Mod($Time,60)       ;Remove all Minutes past 60
    Return $Time                ;Return the Value
EndFunc
Func TimerGetHrs($hTimer)
    $Time = TimerDiff($Timer)   ;Get the time from the timer
    $Time = Int($Time/3600000)  ;Convert milliseconds to hours
    $Time = Mod($Time,24)       ;Remove all Minutes past 24
    Return $Time                ;Return the Value
EndFunc

Edit: Fixed a slight bug in example

Edited by NerdFencer

_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Share this post


Link to post
Share on other sites

Hey,

For the code above (nice example btw), to make the timer look like 00:00:00 instead of 0:0:0 replace this line...

Return String($CurHr)&":"&String($CurMin)&":"&String($CurSec)oÝ÷ ØZ½ëayú%"(ëb¢x­«'¢wuú+Zj[ºÚ"µÍÕRPÜX]J    ][ÝÕ[Y][ÝÊBÌÍÚX[HÕRPÝÜX]SX[
    ][ÝÔÙXÛÛÎ ][ÝËLL
BÕRTÙ]Ý]J
BÌÍÚU[YH[Y[]

HÈÝ[[HÕRQÙ]ÙÊ
H   ÉÝÈLÂßWÔÙXÛÛÊ ÌÍÚX[    ÌÍÚU[YBWÕ[Y[YJ  ÌÍÚU[Y   ÌÍÚX[
BTÛY
JBÑ[B[ÈÕ[Y[YJ    ÌÍÚU[Y[] ÌÍÚÝHY][
BRYÝÝ[ÒÒ[
    ÌÍÚU[Y[]
H[]Ù]ÜK
BSØØ[ ÌÍÚQYH[YY    ÌÍÚU[Y[]
BØØ[  ÌÍÚTÙXÜÈH[Ù
[
    ÌÍÚQYÈL
K

BBBØØ[    ÌÍÚSZ[H[Ù
[
    ÌÍÚQYÈ

K

BBØØ[ ÌÍÚRÝÈH[Ù
[
    ÌÍÚQYÈÍ
K

BBBSØØ[   ÌÍÜÕ[YHÝ[ÑÜX]
    ][ÝÉLLL   ][ÝË  ÌÍÚRÝË ÌÍÚSZ[   ÌÍÚTÙXÜÊBRY   ÌÍÚÝ[   ÌÍÜÕ[Y  ÉÝÈÕRPÝXY
    ÌÍÚÝ
H[ÕRPÝÙ]]J   ÌÍÚÝ    ÌÍÜÕ[YBT]   ÌÍÜÕ[Y[[

Share this post


Link to post
Share on other sites

Hey,

For the code above (nice example btw), to make the timer look like 00:00:00 instead of 0:0:0 replace this line...

That return line does work, and looks better :)

What the globals in my example are for a workaround for the flicker glitch (which is present in your example)

There is a way without globals that I know of, but it is more complex.


_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

Share this post


Link to post
Share on other sites

I didnt notice any flickering, but could only bring myself to watch it for 30 secs at a time or so:P

anyway your code is more versatile, think the OP should have everything he needs with that :)

Share this post


Link to post
Share on other sites

Thank you all for the great examples. Only issue I am running into now is that I put this inside my already existing While statement, but the timer doesn't start going (assuming cause it has to runt through the entire script to get back to the timer part). But I do need it inside my current While statement, I was trying to use Function, but that was not working as I thought it would. If you have any thoughts, that would be great.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@logcomptechs

Maybe...

$init = TimerInit()

While 1
$diff = TimerDiff($init)
TrayTip('Timer', $diff, 0, 1)
If $diff >= 5 Then
;restart timer
; Timer() could be used with a func
$init = TimerInit()
EndIf
WEnd

Not tested.

Cheers, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0