can I cancel the timer within the function I use with timer.performWithDelay?

Is it ok to cancel a timer like this?   (i.e. from a memory managmeent point of view)

&nbsp;&nbsp;&nbsp;&nbsp;local t = timer.performWithDelay(200, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local x = 123 &nbsp;-- do stuff &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.cancel(t) -- \<== IS THIS OK HERE? -- Do I need either of these lines? t:removeSelf() t = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;) &nbsp;

Also do I need:

 - a “removeSelf” here also?

 - a “= nil” here also?

oh, that doesn’t seem to work - I think it would need to be like this:

&nbsp;&nbsp;&nbsp;&nbsp;local t = timer.performWithDelay(200, startPhysicsListener) &nbsp;&nbsp;&nbsp;&nbsp;local function startPhysicsListener(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;physics.start() &nbsp;-- i.e. do stuff &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.cancel(t) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = nil &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;

So again is this acceptable / best practice re memory management for timers?

damm, that doesn’t seem to work either as “startPhysicsListener” isn’t defined at the time of the 1st line…

Any suggestions re how to cancel a timer in the same “area/block” of code you create it in?  i.e. trying to avoid having to put them all into a table to come back at a later time (e.g. scene:exitScene()) to cancel them…

First of all, you can call object:removeSelf() only on display objects and NOT on timers.

For deleting timers just call timer.cancel(timer_name) and timer_name = nil.

Also you don’t need to cancel a timer after it ran out. I mean when the timer did his job, there is nothing to cancel :wink:

To your problem, try something like this:

local t &nbsp; local function startPhysicsListener() &nbsp;&nbsp; physics.start() &nbsp;-- i.e. do stuff &nbsp;&nbsp; timer.cancel(t)&nbsp; --if you really need it, but I don't see for what &nbsp;&nbsp; t = nil end &nbsp; t = timer.performWithDelay(200, startPhysicsListener)

ok thanks - do you know what the “t=nil” does?  does that just ensure the garbage collector is triggered straight away to recover the memory vs later on?

It frees that part of memory, so the garbage collector knows it can be deleted (or overwritten), as far as I know.

oh, that doesn’t seem to work - I think it would need to be like this:

&nbsp;&nbsp;&nbsp;&nbsp;local t = timer.performWithDelay(200, startPhysicsListener) &nbsp;&nbsp;&nbsp;&nbsp;local function startPhysicsListener(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;physics.start() &nbsp;-- i.e. do stuff &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.cancel(t) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = nil &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;

So again is this acceptable / best practice re memory management for timers?

damm, that doesn’t seem to work either as “startPhysicsListener” isn’t defined at the time of the 1st line…

Any suggestions re how to cancel a timer in the same “area/block” of code you create it in?  i.e. trying to avoid having to put them all into a table to come back at a later time (e.g. scene:exitScene()) to cancel them…

First of all, you can call object:removeSelf() only on display objects and NOT on timers.

For deleting timers just call timer.cancel(timer_name) and timer_name = nil.

Also you don’t need to cancel a timer after it ran out. I mean when the timer did his job, there is nothing to cancel :wink:

To your problem, try something like this:

local t &nbsp; local function startPhysicsListener() &nbsp;&nbsp; physics.start() &nbsp;-- i.e. do stuff &nbsp;&nbsp; timer.cancel(t)&nbsp; --if you really need it, but I don't see for what &nbsp;&nbsp; t = nil end &nbsp; t = timer.performWithDelay(200, startPhysicsListener)

ok thanks - do you know what the “t=nil” does?  does that just ensure the garbage collector is triggered straight away to recover the memory vs later on?

It frees that part of memory, so the garbage collector knows it can be deleted (or overwritten), as far as I know.