Question on cancel timers

Hello:

I have to cancel the timers I have at some point.

I use many functions with timer.performWithDelay(  )

then I check if I am using the timer, so I can cancel it.

                if t1 ~= nil then
                    timer.cancel(t1)
                end

it works perfect if I have 3 or 5 timers I do this

                if t1 ~= nil then
                    timer.cancel(t1)
                end

5 times

                if t1 ~= nil then
                    timer.cancel(t1)
                end
                if t2 ~= nil then
                    timer.cancel(t2)
                end
                if t3 ~= nil then
                    timer.cancel(t3)
                end
                if t4 ~= nil then
                    timer.cancel(t4)
                end
                if t5 ~= nil then
                    timer.cancel(t5)
                end

but now I have 60 times, just imagine that long line in my code

QUESTION…

How can I do that with less lines of code? a loop? a function? what?

Thanks for all your help

Victor

Hi Victor:

You can place all your timers in a table and iterate throigh that table in a for-loop, which will condense your code considerably. Or, you might want to check out a module I created (it’s free) that modifies Corona’s timer library in a way that allows you to cancel all active timers at once by simply calling timer.cancel(): http://www.jasonschroeder.com/2015/02/25/timer-2-0-library-for-corona-sdk/

Good luck!

Thanks for this information.

Let me see if I understand it.

When I create a timer I put it on a variable like this

t16 = timer.performWithDelay(tempo*16, beat16)

so then I have to cancel – t16 – like this

         if t16 ~= nil then
               timer.cancel(t16)
         end

So where do I use myTag?

Should I do this first

myTag16 = timer.performWithDelay(tempo*16, beat16)

and so on?

I don’t really understand it.

Thanks for your help

Hi Victor,

Assuming you are using my “timer2” module, then here’s what you could do:

  1. You can create a timer like t16 = timer.perforomWithDelay(tempo*16, beat16), and then cancel it as you indicated - but that would require a lot of code to cancel a whole bunch of timers at once.
  2. You can create a bunch of timers using the same method as above, and cancel them all at once by simply calling timer.cancel() with no argument - that’s a feature of my “timer2” module. You don’t need to check to see if the timer is nil or not - the module will do that for you.
  3. You can add a tag to your timers so that you can cancel all your timers with a specific tag at once (instead of all timers). For example, the below code will create 4 timers, but only 3 of them will have the tag “beatTimers” so if we call timer.cancel(“beatTimers”) only those 3 timers will be cancelled, leaving the 4th one running:

[lua]

local t16 = timer.performWithDelay(tempo*16, beat16, “beatTimers”)

local t17 = timer.performWithDelay(tempo*17, beat17, “beatTimers”)
local t18 = timer.performWithDelay(tempo*18, beat18, “beatTimers”)

– create another timer with no tag
local anotherTimer = timer.performWithDelay(8000, anotherFunction)

– cancel only the beat timers:
timer.cancel(“beatTimers”)

[/lua]

Hope this helps!

Thank you very much, it works!!!

I will change my code to use this timer2.lua works great!

thanks for everything…

Victor

Well… something happens with the timer2…

I just add a newTimer

timer.performWithDelay(2000, playButton)

and it gives me ERRORS

File: timer2.lua
Line: 85
Attempt to index local ‘newTimer’ (a nil value)

is not letting me put more timers, why?

it was working and still is working with the timers I HAD before the “require” of timer2

could you lease help me out

thanks

Hi Victor,

I’d need to see more code to really know what’s going on here. Line 85 of the module appends a tag (even if that tag is nil) to a timer created in line 84 - but if the timer isn’t created successfully then you’d get this error. So I suspect that calling "timer.performWithDelay(2000, playButton) would throw up an error even if you didn’t require the module. But that’s just a guess.

Here are some questions that might help you get to the bottom of things:

  • Do you require the timer module mid-way through your code? It sounds like you have some timers created using the build-in timer library and some using the “timer2” library. That’s not how it’s intended to work. You should just require timer2 once, preferably near the top of your main.lua file.
  • Is “playButton” a function? It sounds like the type of variable name you’d give to a display object. If it is a display object, does it have a “timer” method applied to it? If not, then you can’t use it when calling timer.performWithDelay - that might be your problem right there.
  • The fact that “newTimer” is a nil value means that the timer isn’t successfully being created, which points to some sort of bug in your code. Try creating that same timer using the default timer library and see if it successfully triggers your “playButton” function or method after 2000ms. If not, then you know the problem is with your code.

Good luck!

Thanks… I will try many things. And also if this happens somewhere else also.

Thanks for your tips… they help

Hi Victor:

You can place all your timers in a table and iterate throigh that table in a for-loop, which will condense your code considerably. Or, you might want to check out a module I created (it’s free) that modifies Corona’s timer library in a way that allows you to cancel all active timers at once by simply calling timer.cancel(): http://www.jasonschroeder.com/2015/02/25/timer-2-0-library-for-corona-sdk/

Good luck!

Thanks for this information.

Let me see if I understand it.

When I create a timer I put it on a variable like this

t16 = timer.performWithDelay(tempo*16, beat16)

so then I have to cancel – t16 – like this

         if t16 ~= nil then
               timer.cancel(t16)
         end

So where do I use myTag?

Should I do this first

myTag16 = timer.performWithDelay(tempo*16, beat16)

and so on?

I don’t really understand it.

Thanks for your help

Hi Victor,

Assuming you are using my “timer2” module, then here’s what you could do:

  1. You can create a timer like t16 = timer.perforomWithDelay(tempo*16, beat16), and then cancel it as you indicated - but that would require a lot of code to cancel a whole bunch of timers at once.
  2. You can create a bunch of timers using the same method as above, and cancel them all at once by simply calling timer.cancel() with no argument - that’s a feature of my “timer2” module. You don’t need to check to see if the timer is nil or not - the module will do that for you.
  3. You can add a tag to your timers so that you can cancel all your timers with a specific tag at once (instead of all timers). For example, the below code will create 4 timers, but only 3 of them will have the tag “beatTimers” so if we call timer.cancel(“beatTimers”) only those 3 timers will be cancelled, leaving the 4th one running:

[lua]

local t16 = timer.performWithDelay(tempo*16, beat16, “beatTimers”)

local t17 = timer.performWithDelay(tempo*17, beat17, “beatTimers”)
local t18 = timer.performWithDelay(tempo*18, beat18, “beatTimers”)

– create another timer with no tag
local anotherTimer = timer.performWithDelay(8000, anotherFunction)

– cancel only the beat timers:
timer.cancel(“beatTimers”)

[/lua]

Hope this helps!

Thank you very much, it works!!!

I will change my code to use this timer2.lua works great!

thanks for everything…

Victor

Well… something happens with the timer2…

I just add a newTimer

timer.performWithDelay(2000, playButton)

and it gives me ERRORS

File: timer2.lua
Line: 85
Attempt to index local ‘newTimer’ (a nil value)

is not letting me put more timers, why?

it was working and still is working with the timers I HAD before the “require” of timer2

could you lease help me out

thanks

Hi Victor,

I’d need to see more code to really know what’s going on here. Line 85 of the module appends a tag (even if that tag is nil) to a timer created in line 84 - but if the timer isn’t created successfully then you’d get this error. So I suspect that calling "timer.performWithDelay(2000, playButton) would throw up an error even if you didn’t require the module. But that’s just a guess.

Here are some questions that might help you get to the bottom of things:

  • Do you require the timer module mid-way through your code? It sounds like you have some timers created using the build-in timer library and some using the “timer2” library. That’s not how it’s intended to work. You should just require timer2 once, preferably near the top of your main.lua file.
  • Is “playButton” a function? It sounds like the type of variable name you’d give to a display object. If it is a display object, does it have a “timer” method applied to it? If not, then you can’t use it when calling timer.performWithDelay - that might be your problem right there.
  • The fact that “newTimer” is a nil value means that the timer isn’t successfully being created, which points to some sort of bug in your code. Try creating that same timer using the default timer library and see if it successfully triggers your “playButton” function or method after 2000ms. If not, then you know the problem is with your code.

Good luck!

Thanks… I will try many things. And also if this happens somewhere else also.

Thanks for your tips… they help