Reset Timer

I found a timer here:

https://gist.github.com/coronarob/05accd04ce581b81572f

However, how do I reset the timer?

gonna bump this up.

I really need help on this.

Hi Gary. I know its exiting to work with Corona and when your stuck, you want answers quickly. However our forums are contributed to by a great community of volunteers who give their time willingly. We have to give them time to respond. Our forum rules request that you only bump a post no more than once per 24 hours.

Now what do you mean by “reset” the timer? Do you want to cancel it and start a new one from the beginning?

display.setDefault("background", 0.2, 0.2, 0.4 ) -- Keep track of time in seconds local secondsLeft = 20 \* 60 -- 20 minutes \* 60 seconds local countDownTimer = nil local clockText = display.newText("20:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local function resetTimer() timer.cancel(countDownTimer) secondsLeft = 20 \* 60 countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) end -- run them timer countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )

The main changes are to take the “local” off of the countDownTimer and declaring it local at the top. This gives you module wide access to the variable. Then create a function to do the work needed to reset the function: canceling the timer, reseting the number of seconds and starting a new timer.

Rob

When the time runs out :

secondsLeft = 20*60

clockText.text = “20:00”

then start the timer again

timer.performWithDelay( 1000, updateTime, secondsLeft )

Thanks Rob it worked!

@Vince I did try that but when it was reseted, clock was moving at a faster rate.

gonna bump this up.

I really need help on this.

Hi Gary. I know its exiting to work with Corona and when your stuck, you want answers quickly. However our forums are contributed to by a great community of volunteers who give their time willingly. We have to give them time to respond. Our forum rules request that you only bump a post no more than once per 24 hours.

Now what do you mean by “reset” the timer? Do you want to cancel it and start a new one from the beginning?

display.setDefault("background", 0.2, 0.2, 0.4 ) -- Keep track of time in seconds local secondsLeft = 20 \* 60 -- 20 minutes \* 60 seconds local countDownTimer = nil local clockText = display.newText("20:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local function resetTimer() timer.cancel(countDownTimer) secondsLeft = 20 \* 60 countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) end -- run them timer countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )

The main changes are to take the “local” off of the countDownTimer and declaring it local at the top. This gives you module wide access to the variable. Then create a function to do the work needed to reset the function: canceling the timer, reseting the number of seconds and starting a new timer.

Rob

When the time runs out :

secondsLeft = 20*60

clockText.text = “20:00”

then start the timer again

timer.performWithDelay( 1000, updateTime, secondsLeft )

Thanks Rob it worked!

@Vince I did try that but when it was reseted, clock was moving at a faster rate.