Question to timer

I wonder how to add a timer using the correct time to fit an already started timer until end. So they can end at the exact same time.

For example:

I start a timer with performWithDelay(21020,destroy,1)

And then I want to create new timers for example when a button is pushed, BUT the new timers should use the rest of the left time from the first timer, so all the timers end at the exact same time.

How can I do this?

I don’t think that can be done only using the timer api. You could solve this by saving time stamps, but for them to finish at the exact point, you probably need to cancel involved timers at breakpoints and restart them from that point for a duration that you can calculate.

Try

local delay = 10500 -- 10.5 sec local startTime local index=1 local function printTime( event )   local params = event.source.params   local d, st, id = params.delay, params.startTime, params.id   print( 'id='                .. id ..         ' startTime='         .. st ..         ' delay='             .. d ..         ' system.getTimer()=' .. system.getTimer()    ) end local buttonRunNewTimer = display.newRect(                                 display.contentCenterX,                                 display.contentCenterY,                                 100,                                 100                               ) buttonRunNewTimer.touch = function( self, event )   local phase = event.phase   if phase == 'began' then     local st = system.getTimer()     local d  = delay + startTime - st     local t  = timer.performWithDelay( d, printTime, 1 )     index = index + 1     t.params = { id=index, startTime=st, delay=d }   end end buttonRunNewTimer:addEventListener( "touch" ) startTime = system.getTimer() local t = timer.performWithDelay( delay, printTime, 1 ) t.params = { id=index, startTime=startTime, delay=delay }

It is not perfect.

Have a nice  day:)

ldurniat

I would do this by storing the time that the original timer started and then calculating the other timers around that, i.e.

 

local maxTime = 21020 local timers = {} local startTime = system.getTimer() timers[1] = timer.performWithDelay( maxTime, destroy ) -- some time later when you create a new timer timers[2] = timer.performWithDelay( maxTime-(system.getTimer()-startTime), destroy )

(system.getTimer()-startTime) simply gives you the amount of time that has passed since the startTime was taken, i.e. when timers[1] was fired. When you then subtract this time from the maxTime that you gave the initial function, they’ll stop at the same time.

If you use the method above, you could write a safety function that first checks if maxTime-(system.getTimer()-startTime) is actually positive and if it its, then creates a new timer. I haven’t yet tried what happens if you try to create a timer with negative time. :stuck_out_tongue:

Thank you all for your detailed quick help and response! Much appreciated! :slight_smile:

Awesome!

Why do you want the timers to execute at the exact same time? Does Corona guarantuee that multiple timers will trigger at the exact same time?

Maybe better to call the desired code from end of first timer ?

I don’t think that can be done only using the timer api. You could solve this by saving time stamps, but for them to finish at the exact point, you probably need to cancel involved timers at breakpoints and restart them from that point for a duration that you can calculate.

Try

local delay = 10500 -- 10.5 sec local startTime local index=1 local function printTime( event )   local params = event.source.params   local d, st, id = params.delay, params.startTime, params.id   print( 'id='                .. id ..         ' startTime='         .. st ..         ' delay='             .. d ..         ' system.getTimer()=' .. system.getTimer()    ) end local buttonRunNewTimer = display.newRect(                                 display.contentCenterX,                                 display.contentCenterY,                                 100,                                 100                               ) buttonRunNewTimer.touch = function( self, event )   local phase = event.phase   if phase == 'began' then     local st = system.getTimer()     local d  = delay + startTime - st     local t  = timer.performWithDelay( d, printTime, 1 )     index = index + 1     t.params = { id=index, startTime=st, delay=d }   end end buttonRunNewTimer:addEventListener( "touch" ) startTime = system.getTimer() local t = timer.performWithDelay( delay, printTime, 1 ) t.params = { id=index, startTime=startTime, delay=delay }

It is not perfect.

Have a nice  day:)

ldurniat

I would do this by storing the time that the original timer started and then calculating the other timers around that, i.e.

 

local maxTime = 21020 local timers = {} local startTime = system.getTimer() timers[1] = timer.performWithDelay( maxTime, destroy ) -- some time later when you create a new timer timers[2] = timer.performWithDelay( maxTime-(system.getTimer()-startTime), destroy )

(system.getTimer()-startTime) simply gives you the amount of time that has passed since the startTime was taken, i.e. when timers[1] was fired. When you then subtract this time from the maxTime that you gave the initial function, they’ll stop at the same time.

If you use the method above, you could write a safety function that first checks if maxTime-(system.getTimer()-startTime) is actually positive and if it its, then creates a new timer. I haven’t yet tried what happens if you try to create a timer with negative time. :stuck_out_tongue:

Thank you all for your detailed quick help and response! Much appreciated! :slight_smile:

Awesome!

Why do you want the timers to execute at the exact same time? Does Corona guarantuee that multiple timers will trigger at the exact same time?

Maybe better to call the desired code from end of first timer ?