How to make a countdown timer that is in digital clock format?

I need help on how to make a countdown timer!  I need a timer that will countdown from 20 minutes to 0. 

The timer needs to be in digital clock format (20:00).  Any help or tutorials are appreciated! Thanks in advance!

The easiest thing to do is work in seconds.  Then convert that value to a MM:SS display using some math and string formatting.

This actually sounds like a good tutorial for the week.  Let me write something up for you.  The tutorials post on Tuesdays.

Rob

Thanks Rob!  So I need to wait until Tuesday to see the tutorial? Could you give me the code or a tutorial a little sooner? Let me know.  Thanks! 

Well I still have to write the code.

Rob

@rob when will you reply with the code? Let me know…

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

@rob   Thanks So Much!   I appreciate the help!  

Last Questions, 

1.) I need to make the timer keep going even if the user exits out of the app, how can I accomplish this?

2.) How can I set a conditional statement (if/else) based on what the time is on the timer?  

For example, if the timer runs out and is at 0 seconds and 0 minutes, I can make something happen…  Thanks in advance!

First, there is a small bug in the code.  It needs to subtract the second first.  Grab the latest from the URL above.

  1. How to keep the timer going if someone exits out of the app.

I would use the system event that tracks when you get a suspend, resume, start and exit events.  If you get a suspend, pause the timer and if you get a resume event, restart the timer.  That’s pretty easy.  If you get an exit event, you will need to save the secondsLeft to a file in your system.DocumentsDirectory.  Then when you get a system  start event, check to see if that file is there, read it, start a new timer with the amount of time you have.  Make sure to get rid of the file so you won’t restart with that time later.

2.  In the updateTime() function you can put in a conditional test that if secondsLeft hits 0, do something.  Just do the test somewhere after subtracting 1 from the secondsLeft count.

@rob Again, thanks for the fast response!

You answered by second question perfectly!  However, can you be more specific in your first answer?  

Can you explain how to implement the system event that tracks the state of the app? A link towards any tutorial if there is one?  And how would I go about making a file and saving the time to it?  Then, how would it be deleted?  If you don’t mind explaining how to do these things that would be great! Thanks!

We have a bunch of tutorials that cover many topics like this.  They are easiest to find using Google.  For instance,

a quick Google of “Corona Tutorial system events” found this as the first hit:

https://coronalabs.com/blog/2012/05/15/handling-corona-system-events/

The code samples are not formatted to well due to the age of the tutorial and how our website has changed, but the information should still be helpful.

Rob

@rob thanks again for the tutorial you gave me for the system events! 

Last question I swear!

How would I keep the timer running using the

"local function onSystemEvent( event )

if (event.type == “applicationSuspend”) or (event.type == “applicationExit”)" method?  

I understand how to implement the listener but what do I put inside the function to make the timer keep running, even when the app is exited or suspended?

What do I put in the if/else statement? I don’t want the user to have to come back to the app to resume the time! I wan’t the timer to keep running even when user isn’t in app! Thanks!

First you cannot keep it running while the app sleeps or exits. You can capture os.time() when you start your timer and save it as well.  When you resume or re-start, capture os.time() again and subtract the value you saved and that will give you the elapsed seconds since the timer started.  Then some simple math to compare the original timer seconds - elapsed time would be your new amount to set the timer too.

Rob

@rob that is exactly what I need to do.  However, I have no idea how to save os.time() or use math to compare the times to find the elapsed seconds.  If you could write out a basic structure of the code that would be awesome!  I know how to save to a text file and such.  I don’t know how to do it with Os.time() or even how to implement it.  Also, I would need serious help with the math aspect of it :wink:  

Thanks!

Have you tried searching for how to save things?

Rob

I have set-up my saving methods! Now I just need to know how to do the “simple math” you were referring to in your previous comments. …  What is the math to make the changes to the time after the elapsed seconds is calculated? Thanks!

@rob

? Let me know the math please!

Hi @Mgoldberg62401,

Rob just posted a tutorial on this topic. Please check it out:

https://coronalabs.com/blog/2015/04/28/tutorial-implementing-a-countdown-timer/

Brent

Thanks @brent! However I have made my timer already and it is fully functional!  

I now need the timer to have the effect that it continues while the app isn’t running! (exited or suspended)

I am using os.time()…  As of now, when the timer starts, it saves the os.time().  When the app is restarted, it then records the os.time() again and subtracts it from the original value to find the elapsed time in seconds.  

What I need help with:  

How can I change the value of the timer based off of the os.time() elapsed amount of seconds?!

Any help appreciated! Thanks in advance!

When you suspend or exit and an active timer is running, add os.time() to the secondsLeft.  This is when the timer should expire.

When your app resumes read in the value you saved,  subtract that value from the current os.time():   local secondsLeft = loadedTijme - os.time().

if secondsLeft > 0 then

    – start a new timer using secondsLeft

end

Rob