Jump to content

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 post
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 post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By kitoy
      I wanted to make a countdown timer without queueing the countdown message to the next line but the previous data is removed after using GUICtrlSetData. Any thoughts on how to display the previous data/message?

      Here is an example:
      #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $_main = GUICreate("", 501, 313, -1, -1) Global $g_idMemo = GUICtrlCreateEdit("", 2, 2, 496, 274, $ES_AUTOVSCROLL + $ES_READONLY + $WS_VSCROLL) GUICtrlSetData(-1, "") GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New") GUICtrlSendMsg($g_idMemo, $EM_SETREADONLY, True, 0) GUICtrlSetBkColor($g_idMemo, 0xFFFFFF) GUICtrlSetCursor($g_idMemo, -1) GUISetState(@SW_SHOW) HotKeySet('{esc}', "_close") Func _close() Exit EndFunc ;==>_close Func MemoWrite($sMessage = "") GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1) EndFunc ;==>MemoWrite MemoWrite("test message 1") Sleep(1000) MemoWrite("test message 2") Sleep(1000) MemoWrite("test message 3") Sleep(1000) For $i = 5 to 0 Step -1 GUICtrlSetData($g_idMemo, "Program will exit in "&$i&" seconds...") Sleep(1000) Next While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd  
    • By Rfsvieira
      Hello, i havê been reading, and thinking.. Im on my tablet só i WLL try explain what im trying to do....
      Will havê an GUI with 8 timer input that havê to set timer, but wen our if 2 our more ends at some time it havê to run 1, then outher , then outher
      Timer 1, - input 5 minutes, timer 2 - input 10, timer 3 input 15, timer 4 - input 8,......
      At some time,  timer 1 will end ate some time of timer 2 / then it at some time will end at some time timer 2 and 3 
      How i said that wen it happpens first is 2 then 1, then outhers and if 1 is done then 1st is 3, will have more than 9 input boxes that can happen it end at some time, imagine if wen timer 1= 0 function1(), then after end funciona start countdown again, if timer 2 = 0 run function2(), then after end function2 start contdoen again, if both =0 at some time first run 1 then 2, and if wen ends 1, 2,3,4 are =0 :-( :-( :-(  im lost
      This work…???
       
      thank you!
       
      Thanks for time, any ideas??? 
       
    • By CoolBreeze
      It's been a long time since I use autoit, and its time to share something and help to someone.

      Now I just wanna to share something from my work, to contribute to the community.
      This project is made on request from my friend.
      Maybe this will help someone to make something else like NewYear countdown  
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=blue_shutdown.ico #AutoIt3Wrapper_Outfile=Auto ShutDown.Exe #AutoIt3Wrapper_Res_Description=Auto Shutdown for PC #AutoIt3Wrapper_Res_Fileversion=1.1 #AutoIt3Wrapper_Res_LegalCopyright=Boris Kerkez - boriskerkez@hotmail.com #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; AutoIt Version: 3.3.12.0 #include <Constants.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <ButtonConstants.au3> #include <Timers.au3> ; BUG: ako drzis pritisnut taster(broj) i stisnes ENTER moguce je da ce $Button1 ostati NEAKTIVAN posto $input1, $input1 su prazni ; Ako se vreme na PC promeni u toku tajmera, on to nece registrovati zato sto on odbrojava a ne proverava svake sekunde HH:MM Opt("TrayMenuMode", 1) Global $hGUI, $Active = 0, $FirstTime = 1 Global $remaining_min, $remaining_sec Global $timer_countdown, $calc_option = 1, $readInp_H, $readInp_M, $readInp_after_M $appTitle = "Auto ShutDown v1.1" $gui_w = 280 $gui_h = 150 $hGUI = GUICreate($appTitle, $gui_w, $gui_h) GUISetFont(10, 400, 0, "Verdana") ; Start at $Label1 = GUICtrlCreateLabel("Shutdown at (hh:mm):", 6, 8, 200, 20) $Input1 = GUICtrlCreateInput(@HOUR, 190, 5, 35, 24, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_NUMBER)) GUICtrlSetLimit(-1, 2) $Input2 = GUICtrlCreateInput(@MIN, 240, 5, 35, 24, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_NUMBER)) GUICtrlSetLimit(-1, 2) GUICtrlCreateLabel(":", 230, 8, 5, 20) ; After X minutes $Label2 = GUICtrlCreateLabel("Shutdown after (minutes):", 6, 40, 200, 20) $Input3 = GUICtrlCreateInput("", 190, 39, 85, 24, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_NUMBER)) GUICtrlSetLimit(-1, 6) $Label3 = GUICtrlCreateLabel("", 0, 80, $gui_w, 24, $ES_CENTER) GUICtrlSetFont(-1, 14, 600, 0, "Verdana", 5) GUICtrlSetColor(-1, 0x436EEE) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) ; fix label flickering $Button1 = GUICtrlCreateButton("Set", 5, 120, $gui_w-10, 25, $BS_DEFPUSHBUTTON) GUISetState(@SW_SHOW) While 1 If Not $Active Then Local $memoInp_H = $readInp_H, $memoInp_M = $readInp_M, $memoInp_after_M = $readInp_after_M $readInp_H = GUICtrlRead($Input1) $readInp_M = GUICtrlRead($Input2) $readInp_after_M = GUICtrlRead($Input3) If $memoInp_H <> $readInp_H OR $memoInp_M <> $readInp_M OR $memoInp_after_M <> $readInp_after_M Then ; proveri input1 & input2 + input3(mora biti prazan) | moraju da imaju minimalno 1 cifru (max2 ne treba, postavljen je input limit) | trebaju biti u range-u H=0-23, M=0-59 ; If ( StringLen($readInp_H) < 1 OR $readInp_H < 0 OR $readInp_H > 23 OR StringLen($readInp_M) < 1 OR $readInp_M < 0 OR $readInp_M > 59 AND StringLen($readInp_after_M) = 0 ) Then _Button1StateChange("DISABLE") If ($readInp_H < 0 OR $readInp_H > 23) Then GUICtrlSetData($Input1, "") EndIf If ($readInp_M < 0 OR $readInp_M > 59) Then GUICtrlSetData($Input2, "") EndIf Else If ( StringLen($readInp_after_M) = 0 ) Then $calc_option = 1 ; option: at HH:MM Else ; proveri $Input3 da ne bude 0 i da ne pocinje sa 0 If (StringLeft($readInp_after_M, 1) = 0) Then GUICtrlSetData($Input3, "") _Button1StateChange("DISABLE") Else $calc_option = 2 ; option: after X minutes GUICtrlSetData($Input1, "") GUICtrlSetData($Input2, "") EndIf EndIf _Button1StateChange("ENABLE") EndIf ; ;DEBUG ;ConsoleWrite( $readInp_H &":"& $readInp_M &" | opt:"& $calc_option &" |msec:"& @MSEC & @CRLF) EndIf EndIf ; TRAY msg $tMsg = TrayGetMsg() Switch $tMsg Case $TRAY_EVENT_PRIMARYDOWN _ShowGUI() EndSwitch ; GUI msg $gMsg = GUIGetMsg() Switch $gMsg Case $GUI_EVENT_CLOSE ; Stop the countdown _CountdownStop() Exit Case $GUI_EVENT_MINIMIZE ; Move GUI to tray _HideGUI() ; First time message If $FirstTime Then TrayTip($appTitle, $appTitle & " is minimized to the tray. Click on the tray icon here to restore the window.", 10, $TIP_ICONASTERISK) $FirstTime = 0 EndIf Case $Button1 If Not $Active Then _GUI_Ctrls_DISABLE() ; Calculate and register _CalculateRemainingTime() Else _GUI_Ctrls_ENABLE() ; Stop the countdown _CountdownStop() EndIf EndSwitch WEnd Func _ShowGUI() Opt("TrayIconHide", 1) GUISetState(@SW_SHOW) EndFunc Func _HideGUI() Opt("TrayIconHide", 0) GUISetState(@SW_HIDE) EndFunc Func _GUI_Ctrls_ENABLE() GUICtrlSetState($Input1, $GUI_ENABLE) GUICtrlSetState($Input2, $GUI_ENABLE) GUICtrlSetState($Input3, $GUI_ENABLE) GUICtrlSetData($Button1, "Set") $Active = 0 EndFunc Func _GUI_Ctrls_DISABLE() GUICtrlSetState($Input1, $GUI_DISABLE) GUICtrlSetState($Input2, $GUI_DISABLE) GUICtrlSetState($Input3, $GUI_DISABLE) GUICtrlSetData($Button1, "Cancel") $Active = 1 EndFunc Func _Button1StateChange($button1_state = "ENABLE") If $button1_state = "ENABLE" Then If ( GUICtrlGetState($Button1) = 144 ) Then GUICtrlSetState($Button1, $GUI_ENABLE) EndIf Elseif $button1_state = "DISABLE" Then If ( GUICtrlGetState($Button1) = 80 ) Then GUICtrlSetState($Button1, $GUI_DISABLE) EndIf EndIf EndFunc Func _CalculateRemainingTime() Do ; fix bug with milliseconds Until _MSec() <= 999 Switch $calc_option Case 1 ;OPTION: at HH:MM ; ispravi format - dodaj 0 ispred da bude HH:MM If ( StringLen($readInp_H) < 2 ) Then $readInp_H = 0 & $readInp_H EndIf If ( StringLen($readInp_M) < 2 ) Then $readInp_M = 0 & $readInp_M EndIf ; izracunaj preostale minute If ( $readInp_H >= @HOUR ) Then If ( $readInp_H = @HOUR AND $readInp_M < @MIN ) Then $remaining_min = 1440 - ((@HOUR - $readInp_H)*60 + (@MIN - $readInp_M)) ;(24h*60min=1440min) - (H razlika)*60 + (M razlika) ;ConsoleWrite ( " inH = @H AND inM < @M |min:" & $remaining_min &@CRLF) Else $remaining_min = ((@HOUR - $readInp_H)*60 + (@MIN - $readInp_M)) * -1 ;ConsoleWrite ( " inH >= @H |min:" & $remaining_min &@CRLF) EndIf Else $remaining_min = 1440 - ((@HOUR - $readInp_H)*60 + (@MIN - $readInp_M)) ;(24h*60min=1440min) - (H razlika)*60 + (M razlika) ;ConsoleWrite ( " inH < @H |min:" & $remaining_min &@CRLF) EndIf ; konvertuj u sekunde If ($remaining_min = 0) Then $remaining_sec = (60-@SEC) - $remaining_min * 60 Else $remaining_sec = $remaining_min * 60 - @SEC EndIf Case 2 ; OPTION: after X minutes $remaining_sec = $readInp_after_M * 60 - @SEC EndSwitch ; loading string GUICtrlSetData($Label3, "Loading...") ; fix input bug If ( $remaining_sec <= 0 ) Then GUICtrlSetData($Label3, "Don`t hack me bro!") _GUI_Ctrls_ENABLE() Return EndIf ; fix bug with milliseconds $remaining_sec -= 1 ; set timer for countdown function $timer_countdown = _Timer_Settimer($hGUI, 1000, "_Countdown") EndFunc ;==>CalculateRemainingTime Func _CountdownStop() _Timer_KillTimer($hGUI, $timer_countdown) GUICtrlSetData($Label3, "") EndFunc ;==>CountdownStop Func _Countdown($hWnd, $iMsg, $iIDTimer, $iTime) #forceref $hWnd, $iMsg, $iIDTimer, $iTime If $Active Then Local $sec, $min, $hr $sec = Mod($remaining_sec, 60) $min = Mod($remaining_sec / 60, 60) $hr = Floor($remaining_sec / 60 ^ 2) GUICtrlSetData($Label3, "Time left: " & StringFormat("%02i:%02i:%02i", $hr, $min, $sec)) EndIf If $remaining_sec = 300 Then ; 5min TrayTip($appTitle, "Attention: 5 minutes to shudown!", 10, $TIP_ICONEXCLAMATION) Elseif $remaining_sec = 120 Then ;2min TrayTip($appTitle, "Attention: 2 minutes to shudown!", 10, $TIP_ICONEXCLAMATION) Elseif $remaining_sec = 60 Then ;1min TrayTip($appTitle, "Attention: 1 minute to shudown!", 10, $TIP_ICONEXCLAMATION) ElseIf $remaining_sec <= 10 AND $remaining_sec > 0 Then _ShowGUI() GUICtrlSetColor($Label3, 0xEE4000) ElseIf $remaining_sec <= 0 Then _CountdownStop() ;ConsoleWrite("Shutdown " &@MSEC &@CRLF) ;EXECUTE SHUTDOWN Shutdown(1) Exit EndIf $remaining_sec -= 1 EndFunc ;==>Countdown Func _MSec() Local $stSystemTime = DllStructCreate('ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort') DllCall('kernel32.dll', 'none', 'GetSystemTime', 'ptr', DllStructGetPtr($stSystemTime)) $sMilliSeconds = StringFormat('%03d', DllStructGetData($stSystemTime, 8)) $stSystemTime = 0 Return $sMilliSeconds EndFunc ;==>Msec blue_shutdown.ico
      Auto ShutDown.rar
    • By GreenCan
      Edit 2: Complete rewrite of the function, simplified version using a single function and without use of GUICtrlSetOnEvent or AutoItSetOption. Correct centering of message.
      Edit 1: Updated function to correct possible misbehaving AutoItSetOption.
      MsgBox with Visual CountDown
      Self-explanatory I guess...
      GreenCan
      MsgBox_CountDown.au3 (Previous downloads: 53)
    • By billthecreator
      I created a program that allows the user to create a countdown showing days, hours, minutes, and seconds left.

      The colors fade from dark to light. You may also change the color of each column, red, green, or blue. Or shades of grey.

      The program also uses MrCreatoR's

      If you choose not to use the hover effect, just remove the lines of code that uses it.

      It makes it much more stylish with it.



      Countdown.au3

      I made this because of Elder Scrolls V: Skyrim !
×
×
  • Create New...