Cancel timers with a function

Hi guys! I wanted to ask if this technique is a good idea. I’m using enough timers to create a game and I need to group them in a table and cancel all of them in scene:hide with a function.

create a table

--timers local t = {}  --local table for all timers

use some timers

t.tmr1 = timer.performWithDelay(...) t.tmr2 = timer.performWithDelay(...) t.tmr3 = timer.performWithDelay(...) … t.tmr17 = timer.performWithDelay(...) etc

I think that if I am using a table I have a key / values ​​which I can use in the following way to cancel everything at once.

cancel all timers at once

--cancel all timers local function cancelAllTimers()   for k, v in pairs(t) do     timer.cancel(v)   end   k = nil end

I do not receive any errors on the console or on the devices. I just want to know if doing this is a good practice?

***Sometimes I feel doubt about what I do and would like someone with more experience tell me if I’m on the right way or I should continue evaluating other methods***

Thanks in advance

DoDi

Hi DoDi,

There’s no problem with that. It’s not unlike what I do with my “timer2” module, that you may want to check out. Adds support for tagging timers so you can cancel/pause/resume all timers for a given tag simultaneously, or cancel/pause/resume all timers regardless of tag. It’s on GitHub: https://github.com/schroederapps/corona-timer2

@schroederapps
Thanks for your quick response, I’ll take a look!

Hi DoDi,

There’s no problem with that. It’s not unlike what I do with my “timer2” module, that you may want to check out. Adds support for tagging timers so you can cancel/pause/resume all timers for a given tag simultaneously, or cancel/pause/resume all timers regardless of tag. It’s on GitHub: https://github.com/schroederapps/corona-timer2

@schroederapps
Thanks for your quick response, I’ll take a look!