localToGlobal

FLASH BLOG

hey guys, last week i needed to pause several timers and resume them later on. i ran into a problem which i try to explain in an little example.

so let’s say you start a Timer with 1000 milliseconds delay and stop it after 600 milliseconds. if you call Timer.start() again, it starts with a new delay of 1000 milliseconds again. there is no built-in possibility to resume the timer for the remaining 400 milliseconds. i thought “nothing easier than that, just call Timer.pause()”. but there was no such method, so i decided to write my own ExtendedTimer class and add the pause functionality. now i like to share it with you…

> ExtendedTimer.as
package com.fjakobs.utils
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author fjakobs
	 */
	public class ExtendedTimer extends Timer
	{
		private var _startTime : Number;
		private var _initialDelay : Number;
		private var _paused : Boolean = false;

		public function ExtendedTimer(delay : Number,
									  repeatCount : int = 0)
		{
			super(delay, repeatCount);
			_initialDelay = delay;
			addEventListener(TimerEvent.TIMER,
						    onTimer,false,0,true);
		}

		private function onTimer(event : TimerEvent) : void
		{
			_startTime = new Date().time;
			delay = _initialDelay;
		}

		override public function start() : void
		{
			if(currentCount < repeatCount)
			{
				_paused = false;
				_startTime = new Date().time;
				super.start();
			}
		}

		public function pause() : void
		{
			if(running)
			{
				_paused = true;
				stop();
				delay = delay - (new Date().time - _startTime);
			}
		}

		public function get paused() : Boolean
		{
			return _paused;
		}

		public function get initialDelay() : Number
		{
			return _initialDelay;
		}
	}
}
  • Digg
  • del.icio.us
  • Google
  • Blogosphere News
11 Responses to “pause Timer with ExtendedTimer in ActionScript 3.0”
  1. Actionscript Classes » ExtendedTimer Says:

    [...] http://blog.fjakobs.com/archives/101. [...]

  2. thetoke Says:

    I’d rather make it:

    delay = event.target.currentCount;

  3. fjakobs Says:

    why exactly would you do this?

  4. Nikita Says:

    Thanks for code. But there’s little bug - then repeatCount reaches it’s maximum, method start() will trigger anyway. I added:
    override public function start() : void {
    if(currentCount < _repeatCount){
    _paused = false;
    _startTime = new Date().time;
    super.start();
    }
    }
    where _repeatCount is repeatCount from consructor. Now it’s work fine.

  5. grabek Says:

    hey - make it a static function, and you’re a genius! :)

  6. fjakobs Says:

    @Nikita, thank you for the tip, i updated the start method. now the timer won’t trigger if you call start() after the timer reaches it’s maximum repeatCount.

    @grabek, do you think about a TimerManager?

  7. Martin Says:

    Another slight issue I faced is that in :

    override public function start() : void
    {
    if(currentCount < _repeatCount)
    {
    _paused = false;
    _startTime = new Date().time;
    super.start();
    }
    }

    … the “if” will never return true if _repeatCount is 0 since currentCount and and _repeatCount have the same value initally. Appending

    || _repeatCount == 0

    to the if statement solved the problem for me.

  8. Martin Says:

    Oh, and needless to say: Lovely piece of code. Thanks a lot. :)

  9. grabek Says:

    TimerManager - I suppose so! (:

  10. Peter Says:

    Hey, thanks for this class!!! But, I’m newbie in AS3, classes!

    How I use this code in my class document after import it?

    (sorry for my poor english… I’m brazilian)

  11. Peter Says:

    Yep! It’s works fine here now! Thanks!!!

Leave a Reply