Cancel existing timer?

using defolds timer extension
is it possible to somehow cancel timer without defining id variable outside function

here is my code

local timer_magnet_id

function on_message(self, message_id, message, sender)
. . .
   if gotmagnet then timer.cancel(timer_magnet_id) end
		
   timer_magnet_id = timer.seconds(dur, function(self,id)
       gotmagnet = false
       timer.cancel(id)
   end)

   gotmagnet  = true
. . .
end

Since you have multiple timers you really need to track their ids somehow if you wish to cancel them before they are completed. There’s really no way around that fact.

Why do you cancel the timer inside the callback though? When the timer.seconds() callback is invoked there’s really no point cancelling it.

1 Like

thanks for pointing that out