Jump to content

Countdown Timer help me, Please!


Recommended Posts

Hey everyone, I'm trying to build a app for my school and it requires a timer. Here's what I've gotten so far but I don't know how to get any further. 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\nikolas\desktop\autoit\install\koda_1.7.3.0\forms\timer.kxf
$Form_timer = GUICreate("Form_timer", 196, 143, -1, -1)
$timerlbl = GUICtrlCreateLabel("", 40, 10, 103, 41)
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
$startBtn = GUICtrlCreateButton("Start", 10, 60, 81, 31)
$resetBtn = GUICtrlCreateButton("Reset", 100, 60, 81, 31)
$minBtn = GUICtrlCreateButton("+60", 10, 100, 81, 31)
$halfBtn = GUICtrlCreateButton("+30", 100, 100, 81, 31)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $timeValue = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $minBtn
            $timeValue = $timeValue + 60
            GUICtrlSetData($timerlbl, $timeValue)
        Case $halfBtn
            $timeValue = $timeValue + 10
            GUICtrlSetData($timerlbl, $timeValue)
        Case $resetBtn
            $timeValue = 0
            GUICtrlSetData($timerlbl, $timeValue)
        Case $startBtn
            _Run()
    EndSwitch
    WEnd

Func _Run()
    While 1
        Timer()
    WEnd
EndFunc   ;==>_Run

Func Timer()
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $resetBtn
            $timeValue = 0
    EndSwitch
        If $timeValue == 0 Then
            MsgBox(0,'Done','Timer Done')
        ElseIf $timeValue <> 0 Then
            $timeValue = $timeValue - 1
            GUICtrlSetData($timerlbl, $timeValue)
            Sleep(1000)
            Timer()
        EndIf
EndFunc   ;==>Timer

What is left for me to do is transform the seconds into minutes. If anyone could help me it would be great! Thanks!

Link to comment
Share on other sites

This is how I convert seconds to H:M:S

#include "date.au3"
$FutureTime = "2015/11/5 13:10:05"
$seconds = _DateDiff("s", _NowCalc(), $FutureTime)
$iDays = Int($seconds / 86400)
$iHours = Int(($seconds - ($iDays * 86400)) / 3600)
$iMinutes = Int((($seconds - ($iDays * 86400)) - ($iHours * 3600)) / 60)
$iSeconds = Int(((($seconds - ($iDays * 86400)) - ($iHours * 3600) - ($iMinutes * 60)) * 60) / 60)
MsgBox(0, "Time until..", $iDays & " Days, " & $iHours & " Hours, " & $iMinutes & " minutes, " & $iSeconds & " seconds")

Modify this as needed to turn it into a countdown.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

This is how I convert seconds to H:M:S

#include "date.au3"
$FutureTime = "2015/11/5 13:10:05"
$seconds = _DateDiff("s", _NowCalc(), $FutureTime)
$iDays = Int($seconds / 86400)
$iHours = Int(($seconds - ($iDays * 86400)) / 3600)
$iMinutes = Int((($seconds - ($iDays * 86400)) - ($iHours * 3600)) / 60)
$iSeconds = Int(((($seconds - ($iDays * 86400)) - ($iHours * 3600) - ($iMinutes * 60)) * 60) / 60)
MsgBox(0, "Time until..", $iDays & " Days, " & $iHours & " Hours, " & $iMinutes & " minutes, " & $iSeconds & " seconds")

Modify this as needed to turn it into a countdown.

 

 

I've seen that before but never actually gotten it to work, that's why I kinda leaned into the forums for a little help you know? 

Edited by niksigouin
Link to comment
Share on other sites

That script converts seconds to days, hours, minutes and seconds and places the values into variables. There really isn't an easier way to do it.

Here is the same snippet that I made into a working countdown timer. It's not highly accurate, but it works with a small margin of error in the countdown time.

#include "date.au3"
Global $aTimeLeft
Global $Seconds = 600 ; seconds in 10 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

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...