Countdown Timer, problems with buttons pause/cancel

Hi everyone.

I’m new with corona sdk and a newbie programming with lua code.

My first lines of code are about a countdown timer, and the objective is to create a timer settable by user.

Here is the lines of code:

display.setStatusBar(display.HiddenStatusBar) local widget = require( "widget" ) local button1,button2,clockTimer local runMode = true -- for Pause/Resume button state local started = false --The number of minutes remaining local minuteField = display.newText( "", 0, 0, native.systemFontBold, 50) minuteField:setTextColor( 20, 0, 0, 65 ) minuteField.x, minuteField.y = 150, 50 local secondField = display.newText( "", 0, 0, native.systemFontBold, 50) secondField:setTextColor( 20, 0, 0, 65 ) secondField.x, secondField.y = 200, 50 --Function to update the countdown clock local function updateTime() local time = os.date("\*t") local minuteText = 60 - time.min if (minuteText \< 10) then minuteText = "0" .. minuteText end minuteField.text = minuteText local secondText = 60 - time.sec if (secondText \< 10) then secondText = "0" .. secondText end secondField.text = secondText end local buttonHandler1 = function( event ) local result if runMode then button1:setLabel( "Resume" ) runMode = false result = timer.pause( clockTimer ) else button1:setLabel( "Pause" ) runMode = true result = timer.resume( clockTimer ) end if started == false then button1:setLabel( "Pause" ) runMode = true secondText = "0" minuteText = "0" clockTimer = timer.performWithDelay( 1000, updateTime, -1 ) started = true end end -- Cancel timer local buttonHandler2 = function( event ) local result, result1 result, result1 = timer.cancel( clockTimer ) button1:setLabel( "Start" ) started = false secondText = "0" minuteText = "0" end button1 = widget.newButton { id = "button1", defaultFile = "buttonBlue\_100.png", overFile = "buttonBlueOver\_100.png", label = "Pause", font = native.systemFontBold, fontSize = 22, emboss = true, onRelease = buttonHandler1, } button2 = widget.newButton { id = "button2", defaultFile = "buttonBlue\_100.png", overFile = "buttonBlueOver\_100.png", label = "Cancel", font = native.systemFontBold, fontSize = 22, emboss = true, onRelease = buttonHandler2, } button1.x = display.contentCenterX; button1.y = 360 button2.x = display.contentCenterX; button2.y = 430 --Update the time once immediately to display the correct time updateTime() -- Update the clock once per second clockTimer = timer.performWithDelay( 1000, updateTime, -1 )

I have some problems that i’m trying to resolve, but if the experts can give me an hand helping me would be great!

The problems are: 

1 - I need to make the timer at 0 at the start, and make the timer change after the user chose the time to countdown. How can i do for make the user able to select time? I saw that selectbox doesn’t exist in lua. Should i use PickerWeel? The current numbers of the timers are only indicative.

2- This timer now run automatically when i open the simulator. I want a stopped timer that run only after i tap on the start button. I saw that if i change to false " started " or " runmode " the timer still run at the start. What should i change?

3- With this timer, if i tap on the " Pause "  button, the Timer go on pause, but  only aesthetically and not logically! Then if i tap on the Resume  button, the Timer will resume jumping some number, like if the countdown never paused! How can i do for make the Timer totally in pause?

4-If i tap on the  cancel  button, the timer doesn’t cancel, but the timer goes on pause, but a pause like the pause of the third problem, of course! How can i do for makin the Timer totally cancelled after the tap?

I know that these can seems stupid problems, is my first project and i’m trying to do it better as possible, but i’m new with this programming code, and any help with those problems would be very great!

I’m sorry for the incovenience and for the not-perfect written post! Really thanks to everyone who will answer this post!

Hi everyone, 

i wanna know if i could have a your point of view about this, an help would be really appreciate.

Thank you anyway

You shouldn’t start the timer until you’re ready to start it.  If you need to keep track of what state you’re in, you can use a simple variable to track the state of the timer. 

As for why the time jumps, you’re using os.time() which is returning the number of seconds since Jan 1, 1970.  This value cannot be paused;  You should use a simple counter and on each timer tick (which is 1 second), add one to your counter and use that counter to feed your time display.  You might have to do some math to get minutes and seconds from the counter, but it’s not difficult.

As for selecting the value, on iOS pickerwheel is the select box.  It would be your best widget for iOS use.  We don’t have any android specific picker widgets.  So the pickerWheel stays your best bet.

Rob

Thank you so much Rob!

Your answer helped me, and i saw that you made also a little tutorial on this type of countdown timers, very helpful! Thank you!

The tutorial will publish tomorrow.  Where did you see it already?

Rob

I followed this similar post:

https://forums.coronalabs.com/topic/56500-how-to-make-a-countdown-timer-that-is-in-digital-clock-format/

and i saw the codes you wrote and the links you posted ( i assumed that https://gist.github.com/coronarob/05accd04ce581b81572f was the tutorial)

I’ll stay tuned then if today there will be a tutorial, 'cause i’m still workin on my codes, i have now my countdown timer;

on the logic i didn’t change nothing on buttons functions from the previous code, pause/resume is solved, cancel button still goes on the previous timer, then if i press cancel at 19:50, the display show me 00:00, after i press start, the timer goes on the previous timer, so it show me 19:49…19:48…without creating a new one.

So i’m trying to resolve this, then i’ll just need to make the timer settable by an user ( so i think i’ll go for the pickerweel).

I’ll stay tuned and thank you for everything.

The stuff on github is just the source code.  The tutorial will post later this afternoon.

Rob

Hi everyone, 

i wanna know if i could have a your point of view about this, an help would be really appreciate.

Thank you anyway

You shouldn’t start the timer until you’re ready to start it.  If you need to keep track of what state you’re in, you can use a simple variable to track the state of the timer. 

As for why the time jumps, you’re using os.time() which is returning the number of seconds since Jan 1, 1970.  This value cannot be paused;  You should use a simple counter and on each timer tick (which is 1 second), add one to your counter and use that counter to feed your time display.  You might have to do some math to get minutes and seconds from the counter, but it’s not difficult.

As for selecting the value, on iOS pickerwheel is the select box.  It would be your best widget for iOS use.  We don’t have any android specific picker widgets.  So the pickerWheel stays your best bet.

Rob

Thank you so much Rob!

Your answer helped me, and i saw that you made also a little tutorial on this type of countdown timers, very helpful! Thank you!

The tutorial will publish tomorrow.  Where did you see it already?

Rob

I followed this similar post:

https://forums.coronalabs.com/topic/56500-how-to-make-a-countdown-timer-that-is-in-digital-clock-format/

and i saw the codes you wrote and the links you posted ( i assumed that https://gist.github.com/coronarob/05accd04ce581b81572f was the tutorial)

I’ll stay tuned then if today there will be a tutorial, 'cause i’m still workin on my codes, i have now my countdown timer;

on the logic i didn’t change nothing on buttons functions from the previous code, pause/resume is solved, cancel button still goes on the previous timer, then if i press cancel at 19:50, the display show me 00:00, after i press start, the timer goes on the previous timer, so it show me 19:49…19:48…without creating a new one.

So i’m trying to resolve this, then i’ll just need to make the timer settable by an user ( so i think i’ll go for the pickerweel).

I’ll stay tuned and thank you for everything.

The stuff on github is just the source code.  The tutorial will post later this afternoon.

Rob