Canceling a timer.performWithDelay routine based on an event

I am calling a function using timer.performWithDelay. As you can see the iterations is set to 0 so that it will continue to run.

timer.performWithDelay (3000, myroutine,0)

I also have a button bar that contains 3 buttons, each one changes the scene.

When the routine starts and I click a button it continues to iterate through the routine without stopping.

So my question, I know there is a timer.cancel but I’m not sure where that function goes. Would the timer.cancel go in the onBtnPress function?

Or should I try to use the approach outlined in the document whereby I check for onBtnPress event and cancel within myroutine?

local t = {}
function t:timer( event )
local count = event.count
print( “Table listener called " … count … " time(s)” )
if count >= 3 then
timer.cancel( event.source ) – after 3rd invocation, cancel timer
end
end

– Register to call t’s timer method an infinite number of times
timer.performWithDelay( 1000, t, 0 )

You can also timer.cancel() timers using a handle:

[lua]

local someTimer = timer.performWithDelay(1000, something, 0)

– Later

timer.cancel(someTimer)

[/lua]

  • C

That works thanks.

Any ideas on the correct format to check if a button has been pressed within myroutine? Something like:

myroutine()

if button has been pressed then – not sure how to check this?

timer.cancel

end

step 1

step 2

end

If you create a timer handle, you can stop the timer  when the button is pressed , instead of waiting for the event to fire (unless you want it to).

Like so:

[lua]

local eventTimer = timer.performWithDelay(3000, myroutine, 0)

button.onRelease = function()

  timer.cancel(eventTimer) – Make the timer stop

  – blah blah blah

  – whatever

end

[/lua]

If you still want to check if the button is pressed, you can do something like this:

[lua]

button.onRelease = function()

  button.hasBeenPressed = true

  – blah blah blah

  – whatever

end

local myroutine = function()

  if button.hasBeenPressed then

    – Whatever should happen if the button’s been pressed

  end

  – blah blah blah

  – whatever

end

[/lua]

  • Caleb

I’ll try those out and post the results. Thanks Caleb, much appreciated.

You can also cancel a timer within itself.

For example:

local function myTimer( event ) If myVal == true then timer.cancel( event.source ) end end timer.performWithDelay( 1000, myTimer, 0 )

You can also timer.cancel() timers using a handle:

[lua]

local someTimer = timer.performWithDelay(1000, something, 0)

– Later

timer.cancel(someTimer)

[/lua]

  • C

That works thanks.

Any ideas on the correct format to check if a button has been pressed within myroutine? Something like:

myroutine()

if button has been pressed then – not sure how to check this?

timer.cancel

end

step 1

step 2

end

If you create a timer handle, you can stop the timer  when the button is pressed , instead of waiting for the event to fire (unless you want it to).

Like so:

[lua]

local eventTimer = timer.performWithDelay(3000, myroutine, 0)

button.onRelease = function()

  timer.cancel(eventTimer) – Make the timer stop

  – blah blah blah

  – whatever

end

[/lua]

If you still want to check if the button is pressed, you can do something like this:

[lua]

button.onRelease = function()

  button.hasBeenPressed = true

  – blah blah blah

  – whatever

end

local myroutine = function()

  if button.hasBeenPressed then

    – Whatever should happen if the button’s been pressed

  end

  – blah blah blah

  – whatever

end

[/lua]

  • Caleb

I’ll try those out and post the results. Thanks Caleb, much appreciated.

You can also cancel a timer within itself.

For example:

local function myTimer( event ) If myVal == true then timer.cancel( event.source ) end end timer.performWithDelay( 1000, myTimer, 0 )