Pause Timer for AS3


Pause Timer SourceExample Source

EDIT 05/07/2012:  Made a small changed so getting pausetimer.delay return the initially set delay vs the super.timer delay.  When paused, the pausetimer will update the super.timer delay for the current interval after which it sets it back to the originally set delay.  If you were trying to get the delay before the update, you were seeing this short delay.  The change is outlined here.

Recently I wanted to add a pause function in another tool that I’ll be posting up on here in the future ^_~

Timers were the backbone of the sequencing the tool used and I was pretty surprised to find a lack of pausing ability in the native AS3 Timers.  I looked around to see if anybody had remedied this problem, and Lewinzki did.

The solution was there but I had a few issues with the implementation:

I didn’t like that the delay wouldn’t be reset back to the userset delay (or user updated value) after it had been paused. Basically the timer ticks will be messed up if you didn’t reset the delay back to userset value every tick.

Also if you wanted to attach events to the timer, you actually had to step into the object and listen to the child timer in Lewinzki’s timer.

Other than those two qualms it was sound.  I decided to rewrite it extending the native Timer so its would function exactly the same with the added pause and resume features, though calling start when paused will also just resume as well.

Here is live sample, basically the light blinks every second and if you hit pause it will stay in its state.  Try to anticipate when its going to change, and pause it just before it changes state.  When you resume, the light should switch states quickly. Then pause it right after it changes state, when resuming the light should take a bit more time to switch states.

Pause Timer SourceExample Source

  • Pingback: AS3 Sound Sequencer « gltovar.mind.shift()()

  • Russ

    thanks for this.. solved a little timing bug I had

  • Mark

    Great work. For now i will use this class instead the standard timer class!

    Thanks for sharing.

  • Thank you for sharing this!

    Helped me alot.

  • edge

    Thanx a lot!

  • Yury

    Hey thank you a lot! I used your class in my game:)

  • Oifi


    • Anonymous

      care to elaborate? I’d love to fix sometjhing, if there is a major problem with it.

  • Fernando Ramallo

    Sorry to revive an old thread, but I found this line 

    102: _normalTimerDelay = value;

    was changing the original delay, therefore reducing the delay after every pause and resume, instead of resetting it to the original value after the modified delay passed.
    Was this intentional?

    • gltovar

      I see, if you are attempted to  ‘get delay()’  and its paused you aren’t going to be getting the original delay, but the delay that the timer has been shortened to to allow the pause, but that value isn’t the actual delay. after one interval it should set the delay back to what it was initially set to on the ‘set delay()’   

      Instead of returning the super.delay, i should be returning _normalTimerDelay

    • gltovar