Sign in to follow this  
Followers 0
Prab

Pause a countdown

6 posts in this topic

#1 ·  Posted (edited)

Hi,

I'm writing a countdown timer, but am having trouble getting it to pause. I have trimmed down my script as much as possible, but it is still pretty large. The main line in question is $startTime = $newStartTime - $savedTime. I can get the script to come closer to working by changing it to $startTime = $newStartTime - $savedTime * 10000000, but that doesn't feel right (and doesn't work). Thanks for your help.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include <file.au3>

Dim $startTime = -1
Dim $maxTime = 300
Dim $oldDisplay = ""
Dim $started = False
Dim $savedTime = -1

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 804, 454, 192, 114)
$Label1 = GUICtrlCreateLabel("5:00", 8, 8, 783, 300)
GUICtrlSetFont($Label1, 240)
$Button1 = GUICtrlCreateButton("Start/Stop", 8, 352, 97, 89, $WS_GROUP)
GUICtrlSetOnEvent($Button1, "Start")
GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    If $started Then
        If $startTime <> 0 Then
            $miliSecondsPassed = TimerDiff($startTime)
            $secondsPassed = $miliSecondsPassed / 1000
            $timeRemaining = $maxTime - $secondsPassed
            $minRemaining = Floor($timeRemaining / 60)
            $secRemaining = Floor(Mod($timeRemaining, 60))
            
            $display = $minRemaining & ":" & $secRemaining
            
            GUICtrlSetData($Label1, $display)
            
        EndIf
    EndIf
    Sleep(100)
WEnd

Func Start()
    If $started Then;button acts as stop
        $savedTime = TimerDiff($startTime)
    Else
        If $savedTime <> -1 Then;button acts as resume
;_FileWriteLog("timer.log",$savedTime & " " & $startTime)
            $newStartTime = TimerInit()
            $startTime = $newStartTime - $savedTime
;_FileWriteLog("timer.log", $newStartTime & " " & $startTime)
        Else;button acts as start
            $startTime = TimerInit()
        EndIf
    EndIf
    $started = Not $started
EndFunc

Func Close()
    Exit
EndFunc

Edit1: When I trimmed it down I introduce the flicker and some other minor display bugs.

Edit2: I tried replacing all the TimerInit() and TimerDiff() with _Timer_Init() and _Timer_Diff(), but the results are the same.

Edited by Prab

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Check my improvement :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include <file.au3>

Dim $startTime = -1
Dim $maxTime = 300
Dim $oldDisplay = ""
Dim $started = False
$savedTime = 0

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 804, 454, 192, 114)
$Label1 = GUICtrlCreateLabel("5:00", 8, 8, 783, 300)
GUICtrlSetFont($Label1, 240)
$Button1 = GUICtrlCreateButton("Start/Stop", 8, 352, 97, 89, $WS_GROUP)
GUICtrlSetOnEvent($Button1, "Start")
GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    If $started Then
        If $startTime <> 0 Then
            $miliSecondsPassed = TimerDiff($startTime) + $savedTime
            $secondsPassed = $miliSecondsPassed / 1000
            $timeRemaining = $maxTime - $secondsPassed
            $minRemaining = Floor($timeRemaining / 60)
            $secRemaining = Floor(Mod($timeRemaining, 60))
            
            $display = $minRemaining & ":" & $secRemaining
            
            GUICtrlSetData($Label1, $display)
            
        EndIf
    EndIf
    Sleep(100)
WEnd

Func Start()
    If $started Then;button acts as stop
        $savedTime = $savedTime + TimerDiff($startTime) 
    Else
        $startTime = TimerInit()
    EndIf
    $started = Not $started
EndFunc

Func Close()
    Exit
EndFunc

Start/resume works fine.

EDIT: When the seconds are 1-digit, it looks bad, I'll leave that to you.

Edited by Inverted

Share this post


Link to post
Share on other sites

Many thanks. Your solution is simple and elegant. It will only have to add one line to my Resume() code to make it work. I still sort of wonder what went wrong with mine. Oh well.

I already took care of when seconds are <= 9. I just took that part of the code out to shorten the post.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Lol, well, I was in a hurry :

If $savedTime <> 0 Then   ;button acts as resume
            $startTime = TimerInit()
        Else                      ;button acts as start
            $startTime = TimerInit()
        EndIf

:)

I'll edit my previous post ...

Edited by Inverted

Share this post


Link to post
Share on other sites

I noticed, but didn't want to bite the hand that feeds. Again thanks for the help. I'll post the whole script in the example forum when its done. Nothing spectacular, but, hey, its just an example.

Share this post


Link to post
Share on other sites

I noticed, but didn't want to bite the hand that feeds. Again thanks for the help. I'll post the whole script in the example forum when its done. Nothing spectacular, but, hey, its just an example.

You can add a progress bar. You know, for graphical representation of the countdown. Also, you can make the button text change from start to stop accordingly. Oh, well, just some ideas to play with autoit :)

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