Countdown Timer keep working if app is "suspended"

Hi everyone!

I think my issue is easy to explain, i have a countdown timer in digital clock format, and i have to keep the timer working if the app is " suspended " and not closed.

I tried a few different ways like the

if event.type == “applicationSuspend” then 

end

or the loadsave.lua module with json for trying to calculate the time passed in the suspended state for doing the difference, but nothing worked.

There is a way i can do this?

In the suspend event record the value of os.time() i.e. save it. (this assumes you’re countdown timer is of a 1 second resolution) and cancel the timer.  You also need to save the time left in the timer. In the resume event get os.time() again and subtract your saved os.time() value to get how much time elapsed.  Subtract that from the time left in your timer.  If that value is greater than 0, you have time left, and start a new countdown timer with the remainder. If the value is 0 or less, the timer has expired.

Rob

In the suspend event record the value of os.time() i.e. save it. (this assumes you’re countdown timer is of a 1 second resolution) and cancel the timer.  You also need to save the time left in the timer. In the resume event get os.time() again and subtract your saved os.time() value to get how much time elapsed.  Subtract that from the time left in your timer.  If that value is greater than 0, you have time left, and start a new countdown timer with the remainder. If the value is 0 or less, the timer has expired.

Rob