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?
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.
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.
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.