Are there other ways to stop/cancel a timer delay?

So I understand how to create and use timer.performWithDelay(options) but is there a way to cancel this outside of the function that is being called?

I am looking at this tutorial: https://docs.coronalabs.com/api/library/timer/cancel.html and it appears that the only option to cancel a timer is from within the Table Listener Function.

I have an exit button that when pressed, I need to stop the timer. So the only way I can see this working is if I have a boolean variable for the timer function to check every call and to set it to true if the exit button is pressed. Is there a simpler method to do this?

Cheers.

Any timer can be canceled from anywhere as long as you have the timer ID.

(I wouldn’t call that a tutorial.  Its just a piece of sample code in the API docs.)

math.randomseed( os.time() ) local myTimers = {} local function createSomeTimers( delay ) local count = #myTimers + 1 local function tmp() print( "Timer call #", count ) myTimers[count] = nil -- clear so we know it is done/cancelled end myTimers[#myTimers + 1] = timer.performWithDelay( delay, tmp ) end local function cancelRandomTimer() local num = math.random(1,#myTimers) if( myTimers[num] ~= nil ) then timer.cancel( myTimers[num] ) myTimers[num] = nil -- clear so we know it is done/cancelled end end for i = 1, 10 do createSomeTimers( math.random( 1000, 2000 ) ) end cancelRandomTimer() cancelRandomTimer()

Assuming I didn’t typo something, you can run the code above and get a random result each time. 

15:58:07.005 Timer call # 7 15:58:07.125 Timer call # 6 15:58:07.155 Timer call # 1 15:58:07.475 Timer call # 9 15:58:07.777 Timer call # 4 15:58:07.877 Timer call # 8 15:58:07.927 Timer call # 2 15:58:07.927 Timer call # 5

Pay close attention to how I tracked the timer handles and cancelled them elsewhere.

Now that I’ve shown that you can cancel a timer anywhere as long as you have its ID, let me introduce you to a far superior timer:

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

Jason’s timer 2 code is super nice, because it allows you to:

  • Give timers string tags (multiple timers can have the same tag) that can later be used to cancel them w/o knowing the ID
  • Cancel all timers in one fell swoop
  • … more.

Jason Rocks and this module rocks.

When you call timer.performWithDelay() it will return a timer ID value that you can store for future use.

local spawnTimer = timer.performWithDelay(1000, spawnBadGuy, 0)

later you could:

timer.cancel( spawnTimer )

Rob

Thanks for the super fast response.

It looks like you can cancel the timer based on the entire timer object. The API docs made it look like I needed to get into the timer object and pull out the timer ID from it.

Any timer can be canceled from anywhere as long as you have the timer ID.

(I wouldn’t call that a tutorial.  Its just a piece of sample code in the API docs.)

math.randomseed( os.time() ) local myTimers = {} local function createSomeTimers( delay ) local count = #myTimers + 1 local function tmp() print( "Timer call #", count ) myTimers[count] = nil -- clear so we know it is done/cancelled end myTimers[#myTimers + 1] = timer.performWithDelay( delay, tmp ) end local function cancelRandomTimer() local num = math.random(1,#myTimers) if( myTimers[num] ~= nil ) then timer.cancel( myTimers[num] ) myTimers[num] = nil -- clear so we know it is done/cancelled end end for i = 1, 10 do createSomeTimers( math.random( 1000, 2000 ) ) end cancelRandomTimer() cancelRandomTimer()

Assuming I didn’t typo something, you can run the code above and get a random result each time. 

15:58:07.005 Timer call # 7 15:58:07.125 Timer call # 6 15:58:07.155 Timer call # 1 15:58:07.475 Timer call # 9 15:58:07.777 Timer call # 4 15:58:07.877 Timer call # 8 15:58:07.927 Timer call # 2 15:58:07.927 Timer call # 5

Pay close attention to how I tracked the timer handles and cancelled them elsewhere.

Now that I’ve shown that you can cancel a timer anywhere as long as you have its ID, let me introduce you to a far superior timer:

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

Jason’s timer 2 code is super nice, because it allows you to:

  • Give timers string tags (multiple timers can have the same tag) that can later be used to cancel them w/o knowing the ID
  • Cancel all timers in one fell swoop
  • … more.

Jason Rocks and this module rocks.

When you call timer.performWithDelay() it will return a timer ID value that you can store for future use.

local spawnTimer = timer.performWithDelay(1000, spawnBadGuy, 0)

later you could:

timer.cancel( spawnTimer )

Rob

Thanks for the super fast response.

It looks like you can cancel the timer based on the entire timer object. The API docs made it look like I needed to get into the timer object and pull out the timer ID from it.