Runtime listener instead of timer

I want to use a timer for a countdown timer, but if the user suspends the app during the countdown, the timer stops, and starts again when the app is resumed. 

I guess the solution would be to use a Runtime listener. But what should i listen to, enterframe? I want the same performance as of the timer.performWithDelay(1000, countDown, 60). How should i do that?

Is there some way to keep a timer going when the app is suspended? I dont get any to work that i have tried. Notifications is not an option in this case.

No, when the app is suspended, it’s suspended.

When you start the timer, save the current time as well as the amount of time that should elapse. Then do one of the following two things:

  1. When the timer ends, go delete that saved time and do whatever you need to do.

  2. When the app starts (or restarts after being suspended) check and see if there’s a saved timer. If so, load that info and figure out how much time should be left and create a new timer for that (and when creating that, you’ll save *that* current time as well as new amount of time that should elapse).

Maybe that will work for whatever you’re trying to do?

 Jay

A standard ‘delta time’ calculation should do the trick

local prevTime local countTime = 0 local function Update(event) if prevTime == nil then prevTime = event.time end local deltaTime = event.time - prevTime prevTime = event.time countTime = countTime + deltaTime if countTime \> 1000 then countTime = countTime - 1000 countDown() end end Runtime:addEventListener("enterFrame", Update)

Is there some way to keep a timer going when the app is suspended? I dont get any to work that i have tried. Notifications is not an option in this case.

No, when the app is suspended, it’s suspended.

When you start the timer, save the current time as well as the amount of time that should elapse. Then do one of the following two things:

  1. When the timer ends, go delete that saved time and do whatever you need to do.

  2. When the app starts (or restarts after being suspended) check and see if there’s a saved timer. If so, load that info and figure out how much time should be left and create a new timer for that (and when creating that, you’ll save *that* current time as well as new amount of time that should elapse).

Maybe that will work for whatever you’re trying to do?

 Jay

A standard ‘delta time’ calculation should do the trick

local prevTime local countTime = 0 local function Update(event) if prevTime == nil then prevTime = event.time end local deltaTime = event.time - prevTime prevTime = event.time countTime = countTime + deltaTime if countTime \> 1000 then countTime = countTime - 1000 countDown() end end Runtime:addEventListener("enterFrame", Update)