timer.cancel() doesn't work

I’m trying to cancel a timer but it doesn’t get canceled when it should, and each time a touch event occurs, a new timer gets added:

local fireTimer local function onTouch( event ) if( event.phase == "began") then fireTimer = timer.performWithDelay( 3000, fire, 0 ) \<-- works fine elseif( event.phase == "ended" ) then timer.cancel( fireTimer ) \<-- doesn't work, it keeps going, "pause" doesn't work either end end

player:addEventListener( "touch", onTouch )

Because each time you click the button, it will create a new timer…so removing that timer in ended phase wont work…but if you want it to remove, then do following Local fireTouch IsTimerEnabled = true Local function onTouch(event) If event.phase == “began” If isTimerEnabled == true then – perform ur timer End Elseif event.phase == “ended” isTimerEnabled = false – cancel the timer End End

Why?!

If I’m creating something in the “began” phase, why can’t I delete or reverse it in the “ended” phase?

PS: It would be very helpful if you format the code for better readability. 

 

Can you put a:

print( event.phase )

before your if statement to make sure  you’re getting an ended phase.

Rob

The ‘ended’ phase is not always called, like:

  • in events such as user touch going off-screen

  • user touch going onto another ‘touch-object’

I would recommend to put another timer to cancel the fireTimer after a few seconds.

-- Cancel fireTimer after 5seconds timer.performWithDelay( 5000, function() timer.cancel(fireTimer); end )

The “ended” phase always gets executed in my code, it’s the first thing I checked, also putting another timer there is not practical because I don’t know exactly the time I want the first time to be cancelled, I want it to cancel whenever the touch ends.

YOu can do like this in your ended phase 

elseif event.phase == "ended" then if fireTimer then timer.cancel(fireTimer) fireTimer = nil end end

I tested this issue with the below code:

local fireTimer local fire = function() print("printing from a function here") end local function onTouch( event ) print(event.phase) if( event.phase == "began") then fireTimer = timer.performWithDelay( 300, fire, 0 ) elseif( event.phase == "ended" ) then timer.cancel( fireTimer ) end end Runtime:addEventListener( "touch", onTouch )

The timer cancels on the ended phase for me. Test this yourself and see if it also works as intended. If it does, start slowly changing parts of the  working code to operate like your  non-working code. That way, you will find the error you were experiencing.

Yes. It too worked for me !

This definitely helped. Thank you. 

Because each time you click the button, it will create a new timer…so removing that timer in ended phase wont work…but if you want it to remove, then do following Local fireTouch IsTimerEnabled = true Local function onTouch(event) If event.phase == “began” If isTimerEnabled == true then – perform ur timer End Elseif event.phase == “ended” isTimerEnabled = false – cancel the timer End End

Why?!

If I’m creating something in the “began” phase, why can’t I delete or reverse it in the “ended” phase?

PS: It would be very helpful if you format the code for better readability. 

 

Can you put a:

print( event.phase )

before your if statement to make sure  you’re getting an ended phase.

Rob

The ‘ended’ phase is not always called, like:

  • in events such as user touch going off-screen

  • user touch going onto another ‘touch-object’

I would recommend to put another timer to cancel the fireTimer after a few seconds.

-- Cancel fireTimer after 5seconds timer.performWithDelay( 5000, function() timer.cancel(fireTimer); end )

The “ended” phase always gets executed in my code, it’s the first thing I checked, also putting another timer there is not practical because I don’t know exactly the time I want the first time to be cancelled, I want it to cancel whenever the touch ends.

YOu can do like this in your ended phase 

elseif event.phase == "ended" then if fireTimer then timer.cancel(fireTimer) fireTimer = nil end end

I tested this issue with the below code:

local fireTimer local fire = function() print("printing from a function here") end local function onTouch( event ) print(event.phase) if( event.phase == "began") then fireTimer = timer.performWithDelay( 300, fire, 0 ) elseif( event.phase == "ended" ) then timer.cancel( fireTimer ) end end Runtime:addEventListener( "touch", onTouch )

The timer cancels on the ended phase for me. Test this yourself and see if it also works as intended. If it does, start slowly changing parts of the  working code to operate like your  non-working code. That way, you will find the error you were experiencing.

Yes. It too worked for me !

This definitely helped. Thank you.