pausing all timers?

When working with something like this and a LOT of it:

transitionStash[#transitionStash+1]=transition.to (obj,{time=2000,x=200,onComplete=function() timerStash[#timerStash+1]=timer.performWithDelay (1000,startfunction,1) end})

How can I implement a pause function for both timers and transitions?

I can pause all transitions, by using transition.pause() … but for timers I have to pause them individually AND it seems not to work for the above construct… somehow the timers get started even when they should be paused by a for look cycling through the table. I think the pause for timers will only timers which already got started and not those waiting to do their “stuff”.

Has anyone a good and easy to implement solution for this?

Thank you!

Hey c.noeth,

I’m not if you I understand you right, do you try to “pause” timers, that haven’t been started yet?

If yes, that’s not possible (the timer is not created until the onComplete function of the transition is executed) and misses the point.

The way to do it is to loop through all started timers and pause them (as your approach shows). The important part is, that you remove them from the stash after they finished. (this works, I use it myself)

If you want to stop the timers from being created you have to pause the transitions. This also works (I use this one as well).

If that tackles none of your problems, please explain a bit further.

Greetings

Torben :slight_smile:

Thx for your fast reply.

It’s a little bit different… let’s try another example:

How can this following code be “paused” correctly…

timerStash[#timerStash+1]=timer.performWithDelay (1000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (2000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (3000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (4000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (5000,function() startfunction (value) end,1)

let’s say we want to pause the timers after about 1500 miliseconds… this means the first startfunction is already called (after the 1000 miliseconds). When doing this:

for x=#timerStash,1,-1 do

    timer.pause(timerStash[x])

end

in my code the timers are not getting paused and the 2000,3000,4000,5000 milisecond timers are executing the startfunction. Shouldn’t they be paused?

How can I pause them correctly?

Where are you creating timerStash?

What is the value of #timerStash just before your for loop?

It’s a little difficult to give the correct answers because the example code above only shows the basics of my real code…

timerStash={} is set in main.lua

The value for #timerStash should be 5 before starting pausing the timers.

I just wonder when exactly is a timer active? When the timer.performWithDelay (…) is called or when the time in milliseconds inside the brackets is reached?

What is a good way to pause a lot of timers at once?

You’re doing it the way you are supposed to be. I guess I wasn’t clear. Put some print statements in your code and see what the value of #timerStash is.

Is timerStash global? Is it in scope when you try and cancel the timers? This is why I’m curious as to what the value is.

Rob

Thanks for your details Rob! I checked for scope and noticed this was the problem. I created it in main.lua globally but messed the scope up in a module. Thank you very much for giving me the info where to look at! :slight_smile:

Now it is working and I just have to create a check to avoid the warnings, like:

WARNING: timer.pause() cannot pause a timerId that is already expired.
WARNING: timer.pause( timerId ) ignored b/c timerId is already paused.

Jason Schroeder has a library for creating tagged timers so you can pause/cancel timers in a ‘tagged’ group all at once:

http://www.jasonschroeder.com/2015/02/25/timer-2-0-library-for-corona-sdk/#more-174

Thank you for the link! Reads like this is the “thing”! :slight_smile:

Hey c.noeth,

I’m not if you I understand you right, do you try to “pause” timers, that haven’t been started yet?

If yes, that’s not possible (the timer is not created until the onComplete function of the transition is executed) and misses the point.

The way to do it is to loop through all started timers and pause them (as your approach shows). The important part is, that you remove them from the stash after they finished. (this works, I use it myself)

If you want to stop the timers from being created you have to pause the transitions. This also works (I use this one as well).

If that tackles none of your problems, please explain a bit further.

Greetings

Torben :slight_smile:

Thx for your fast reply.

It’s a little bit different… let’s try another example:

How can this following code be “paused” correctly…

timerStash[#timerStash+1]=timer.performWithDelay (1000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (2000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (3000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (4000,function() startfunction (value) end,1)

timerStash[#timerStash+1]=timer.performWithDelay (5000,function() startfunction (value) end,1)

let’s say we want to pause the timers after about 1500 miliseconds… this means the first startfunction is already called (after the 1000 miliseconds). When doing this:

for x=#timerStash,1,-1 do

    timer.pause(timerStash[x])

end

in my code the timers are not getting paused and the 2000,3000,4000,5000 milisecond timers are executing the startfunction. Shouldn’t they be paused?

How can I pause them correctly?

Where are you creating timerStash?

What is the value of #timerStash just before your for loop?

It’s a little difficult to give the correct answers because the example code above only shows the basics of my real code…

timerStash={} is set in main.lua

The value for #timerStash should be 5 before starting pausing the timers.

I just wonder when exactly is a timer active? When the timer.performWithDelay (…) is called or when the time in milliseconds inside the brackets is reached?

What is a good way to pause a lot of timers at once?

You’re doing it the way you are supposed to be. I guess I wasn’t clear. Put some print statements in your code and see what the value of #timerStash is.

Is timerStash global? Is it in scope when you try and cancel the timers? This is why I’m curious as to what the value is.

Rob

Thanks for your details Rob! I checked for scope and noticed this was the problem. I created it in main.lua globally but messed the scope up in a module. Thank you very much for giving me the info where to look at! :slight_smile:

Now it is working and I just have to create a check to avoid the warnings, like:

WARNING: timer.pause() cannot pause a timerId that is already expired.
WARNING: timer.pause( timerId ) ignored b/c timerId is already paused.

Jason Schroeder has a library for creating tagged timers so you can pause/cancel timers in a ‘tagged’ group all at once:

http://www.jasonschroeder.com/2015/02/25/timer-2-0-library-for-corona-sdk/#more-174

Thank you for the link! Reads like this is the “thing”! :slight_smile: