Pause Timer Bug

I’m having problems pausing timers, for example: I have a timer that creates a display object every 2 seconds, but when I pause and resume the timer (for 3/4 seconds), the timer stops to create objects and waits for about 2 seconds and then creates 3 or 4 objects at the same time changing the iteration, but after that the timer starts to work well again. [import]uid: 81091 topic_id: 19647 reply_id: 319647[/import]

Can you provide some plug and play code please?

What version of Corona are you using?

Peach :slight_smile: [import]uid: 52491 topic_id: 19647 reply_id: 75913[/import]

Sure, test it.

local timers = {}  
local gamepause = false  
local timer1  
local i = 0  
   
local function pause (e)  
 if ( gamepause == false and e.phase == "began" ) then  
 timer.pause ( timer1 )  
 for i = 1, #timers do  
 timer.pause (timers[i])  
 end  
 gamepause = true  
 end  
end  
   
Runtime:addEventListener ( "touch", pause )  
   
local function unpause (e)  
 if ( gamepause == true and e.phase == "ended" ) then  
 gamepause = false  
 timer.resume ( timer1 )  
 for i = 1, #timers do  
 timer.resume (timers[i])  
 end  
 end  
end  
   
Runtime:addEventListener ( "touch", unpause )  
   
local function timer2 (e)  
 if ( gamepause == false ) then  
 for k,v in pairs ( timers ) do  
 if ( v == e.source ) then  
 table.remove ( timers, k )  
 print ( "Complete: "..k, e.source, #timers )  
 end  
 end  
 end  
end  
   
local function newTimer ()  
 if ( gamepause == false ) then  
 i = #timers + 1  
 timers[i] = timer.performWithDelay ( math.random ( 0, 10000 ) , timer2, 1 )  
 print ( "Timer: "..i, timers[i], #timers )  
 end  
end  
   
timer1 = timer.performWithDelay ( 400, newTimer, 0 )  

If you pause it for a 3 or 4 seconds, (when there are a lot of timers created, about 12 or 13), sometimes the timer1 stops for seconds the creation of newTimer, and many timers are completed at the same time, but if you pause the game for almost 10 seconds everything goes perfect.

I’m using the last daily build 715. [import]uid: 81091 topic_id: 19647 reply_id: 76004[/import]

Can you file there here, please?
http://developer.anscamobile.com/content/bug-submission

Out of interest, is there a practical reason for creating so many timers at once?

Peach :slight_smile: [import]uid: 52491 topic_id: 19647 reply_id: 76121[/import]

Reported bug, and I need all those timers to spawn many display objects for my game (same object) in random times. [import]uid: 81091 topic_id: 19647 reply_id: 76442[/import]

I believe the problem here isn’t a bug more of a logistical problem.

When you resume the timer it will resume at its last time. So that could create some oddness with your situation.

What I suggest is this.

Create a forward reference for your newTimer.

Cancel timer1 rather than pausing it.

Re-init timer1 on resume

timer1 = timer.performWithDelay ( 400, newTimer, 0 ) [import]uid: 84637 topic_id: 19647 reply_id: 76491[/import]

Oh, I’m sorry, but I didn’t understand, could you change the initial code please? [import]uid: 81091 topic_id: 19647 reply_id: 76659[/import]