Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to pause and resume a timer? — Gideros Forum

How to pause and resume a timer?

bhoubhou Member
edited October 2012 in General questions
In the reference doc, I see there is Timer.pauseAll() and Timer.resumeAll(). But what if I want to pause an individual timer?
I tried stop/start, it does not work
Tagged:

Comments

  • Timer:start and Timer:stop are the correct way to handle this:

    Can you try this code:
    myTimer = Timer.new(100, 0)
    myTimer:addEventListener(Event.TIMER, function () print(myTimer:getCurrentCount()) end)
    myTimer:start()
     
    stage:addEventListener(Event.MOUSE_DOWN, function () myTimer:stop() end)
    stage:addEventListener(Event.MOUSE_UP, function () myTimer:start() end)
    The console should output the count, press the mouse down in the player to call the stop() function and release it to call start() which will resume where it was stopped.

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • @BlastByte

    My code looks like this:
    After the timer counts 10 timers with an interval of 1 second, the code stops counting and do some processing jobs

    The problem happens when the next timer:start() starts the timer, but the timer runs twice faster each 1 second

    The third round makes the timer 3 times faster.

    Any one can explain this? or it is a bug of the timer implementation?
    local timer = Timer.new(1000, 0)
     
    local count = 1
     
    function onTimer(event)
    	print("counter", count)
    	if count == 10 then
    		timer:stop()
    		print("do something!")
    		count = 0
    		timer:start()
    	end
    	count = count + 1
    end
     
    timer:addEventListener(Event.TIMER, onTimer)
     
    timer:start()
  • BlastByteBlastByte Member
    Accepted Answer
    In your example there's no need to stop or start the timer. If you remove timer:stop and timer:start it will run as you expect.

    If at this point you wanted to do something that will take some time then I'd suggest stopping the timer as you have, calling the function and then broadcast an event to start the timer again once the work has been completed.
  • @BlastByte

    Right, start the timer outside the callback could work. What I dont understand is why it does not work inside the callback.

    stop and restart a timer itself inside the callback sounds logical for me : )

    Anyway, thanks!
Sign In or Register to comment.