Getting a button to reset a timer when pressed again

Hi,

I’ve got a simple timer that begins when I press a button:

local function handleButtonEvent( event )

    if ( “ended” == event.phase ) then

    local timeLimit = coolDown

timeLeft = display.newText(timeLimit, 120, 80, native.systemFontBold, 14)

timeLeft:setTextColor(255,0,0)

local function timerDown()

   timeLimit = timeLimit-1

   timeLeft.text = timeLimit

     if(timeLimit==0)then

        print(“cooldown available”)

     end

  end

timer.performWithDelay(1000,timerDown,timeLimit)

    

    

    end

end

coolDown is defined elsewhere, so the code works fine.  The problem is that when I press the button AGAIN, it starts a second timer on top of the first one, ad infinitum.  Is there a way to also make sure that the button press removes any previous instances of the timer?

Thanks

If you want to cancel the timer, then you could save the handle from the timer:

coolDownTimer = timer.performWithDelay(…)

and then in your code you could do something like:

if coolDownTimer then

    timer.cancel( coolDownTimer )

    coolDownTimer = nil

end

inside your phase == “ended” if block.

Rob

local function handleButtonEvent( event )

    if ( “ended” == event.phase ) then

    

        if coolDownTimer then

    timer.cancel( coolDownTimer )

    coolDownTimer = nil

end

    local timeLimit = coolDown

timeLeft = display.newText(timeLimit, 120, 80, native.systemFontBold, 14)

timeLeft:setTextColor(255,0,0)

local function timerDown()

   timeLimit = timeLimit-1

   timeLeft.text = timeLimit

     if(timeLimit==0)then

        print(“cooldown available”) – or do your code for time out

     end

  end

coolDownTimer = timer.performWithDelay(1000,timerDown,timeLimit)    

    

    end

end

That’s my new code, but still the same issue; if I switch scenes (which involves a removeSelf command for the timer), then it’s fine to start the timer with a new coolDown time.  But if I click the button more than once, it still stacks new timers on top of the old (running) ones.

  • Daniel

Just as a test, I added another button to “cancel” the timer:

function handleButtonEvent2( event )

    if ( “ended” == event.phase ) then

    

    timer.cancel(coolDownTimer)

end

   end

And then created the button, and when I click on that button, it PAUSES the timer, but does not remove it.

Is there a way I could do a coolDownTimer:removeSelf() instead, since timer.cancel does not appear to actually remove the timer?

Well I just went crazy and did this:

 if timeLeft then

       timeLeft:removeSelf()

       timeLeft = nil

       timeLimit = nil

       timer.cancel(coolDownTimer)

  end

Now it seems to work :smiley:

Now I have a second problem :smiley:

While I can properly “restart” the timer with the button and cancel the previous timer, this does NOT work if I change between tab/scenes.

When I switch to my other tab/scene to pick a new cooldown timer and come back to this tab to start a new timer, it also redisplays the old timer (which is still counting down).

I don’t want to destroy the counting down timer when I switch scenes, as that is the proper functionality, so that’s not a solution.

I’m just not sure why I’m unable to cancel/remove the previous timer after I have switched to another scene and back again.  I’m assuming it’s now returning my if/then statement as false, but I’m not sure why.

Never mind, solved it all :slight_smile:  Thanks for the help.

If you want to cancel the timer, then you could save the handle from the timer:

coolDownTimer = timer.performWithDelay(…)

and then in your code you could do something like:

if coolDownTimer then

    timer.cancel( coolDownTimer )

    coolDownTimer = nil

end

inside your phase == “ended” if block.

Rob

local function handleButtonEvent( event )

    if ( “ended” == event.phase ) then

    

        if coolDownTimer then

    timer.cancel( coolDownTimer )

    coolDownTimer = nil

end

    local timeLimit = coolDown

timeLeft = display.newText(timeLimit, 120, 80, native.systemFontBold, 14)

timeLeft:setTextColor(255,0,0)

local function timerDown()

   timeLimit = timeLimit-1

   timeLeft.text = timeLimit

     if(timeLimit==0)then

        print(“cooldown available”) – or do your code for time out

     end

  end

coolDownTimer = timer.performWithDelay(1000,timerDown,timeLimit)    

    

    end

end

That’s my new code, but still the same issue; if I switch scenes (which involves a removeSelf command for the timer), then it’s fine to start the timer with a new coolDown time.  But if I click the button more than once, it still stacks new timers on top of the old (running) ones.

  • Daniel

Just as a test, I added another button to “cancel” the timer:

function handleButtonEvent2( event )

    if ( “ended” == event.phase ) then

    

    timer.cancel(coolDownTimer)

end

   end

And then created the button, and when I click on that button, it PAUSES the timer, but does not remove it.

Is there a way I could do a coolDownTimer:removeSelf() instead, since timer.cancel does not appear to actually remove the timer?

Well I just went crazy and did this:

 if timeLeft then

       timeLeft:removeSelf()

       timeLeft = nil

       timeLimit = nil

       timer.cancel(coolDownTimer)

  end

Now it seems to work :smiley:

Now I have a second problem :smiley:

While I can properly “restart” the timer with the button and cancel the previous timer, this does NOT work if I change between tab/scenes.

When I switch to my other tab/scene to pick a new cooldown timer and come back to this tab to start a new timer, it also redisplays the old timer (which is still counting down).

I don’t want to destroy the counting down timer when I switch scenes, as that is the proper functionality, so that’s not a solution.

I’m just not sure why I’m unable to cancel/remove the previous timer after I have switched to another scene and back again.  I’m assuming it’s now returning my if/then statement as false, but I’m not sure why.

Never mind, solved it all :slight_smile:  Thanks for the help.