Jump to content

Count Down - (Moved)


Recommended Posts

Please help calculate the secs variable i can't get it right after 60s, it go into -1 and on....

Func CountDown()
    Local $count = 5
    Local $newcount = $count * 60 ; 300 sec
    Local $time = TimerInit()
    While 1
        Sleep(1000)
        Local $timediff = TimerDiff($time)/1000 ; turn into sec
        Local $mins= Floor(($newcount - $timediff)/60)
        Local $secs = Floor($newcount/$count - $timediff); Need help this line can't calculate after 60s
        ToolTip("Mins = " & $mins & " Secs = " & $secs)
    WEnd
EndFunc

 

Link to comment
Share on other sites

A Google search for "AutoIt Countdown" returns multiple matches, so why reinvent the wheel.

Here an example from @BrewManNH - extended with _Terminate():

#include "date.au3"
HotKeySet("{ESC}", "_Terminate")

Global $aTimeLeft
Global $Seconds = 300 ; seconds in 5 minutes
Global $hTimer = TimerInit()

$aTimeLeft = _TimeConvert($Seconds) ; convert the seconds into an array holding the D:H:M:S
ToolTip("Days:  " & $aTimeLeft[0] & " Hours: " & $aTimeLeft[1] & " Minutes: " & $aTimeLeft[2] & " Seconds: " & $aTimeLeft[3], Default, Default, "Time Left") ; Display the converted seconds in a tooltip
While $Seconds
    If TimerDiff($hTimer) >= 1000 Then ; if a second or more has passed then process these commands
        $aTimeLeft = _TimeConvert($Seconds) ; convert the seconds into an array holding the D:H:M:S
        $Seconds -= 1 ; subtract a second from the countdown
        ToolTip("Days:  " & $aTimeLeft[0] & " Hours: " & $aTimeLeft[1] & " Minutes: " & $aTimeLeft[2] & " Seconds: " & $aTimeLeft[3], Default, Default, "Time Left") ; Display the converted seconds in a tooltip
        $hTimer = TimerInit() ; reset the timer handle
    EndIf
WEnd
ToolTip("")

Func _TimeConvert($iSeconds)
    Local $aTimeSplit[4]
    $aTimeSplit[0] = Int($iSeconds / 86400)
    $aTimeSplit[1] = Int(($iSeconds - ($aTimeSplit[0] * 86400)) / 3600)
    $aTimeSplit[2] = Int((($iSeconds - ($aTimeSplit[0] * 86400)) - ($aTimeSplit[1] * 3600)) / 60)
    $aTimeSplit[3] = Int(((($iSeconds - ($aTimeSplit[0] * 86400)) - ($aTimeSplit[1] * 3600) - ($aTimeSplit[2] * 60)) * 60) / 60)
    Return $aTimeSplit
EndFunc   ;==>_TimeConvert

Func _Terminate()
    ToolTip("")
    Exit
EndFunc   ;==>_Terminate

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

A more readable _TimeConvert function :

Func _TimeConvert($iSeconds)
  Local Const $aDiv = [24, 60, 60]
  Local $aTimeSplit[4]
  For $i = UBound($aDiv) - 1 To 0 Step -1
    $aTimeSplit[$i + 1] = Mod($iSeconds, $aDiv[$i])
    $iSeconds = Floor($iSeconds / $aDiv[$i])
  Next
  $aTimeSplit[0] = $iSeconds
  Return $aTimeSplit
EndFunc   ;==>_TimeConvert

You can always make a better wheel ;)

Link to comment
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
 Share

×
×
  • Create New...