« Updated: Get current week of the year | Home | How to install: flvtool2, flvtool++ and yamdi under Centos »

September 18, 2009

Timer with a pause and resume

I needed the ability to pause the Timer class we have in ActionScript3 so i created my own TimerAdv class which extends it. I tested it on the environment i needed so i'm posting it here for everyone to test it out. Hopefully if i get the chance to properly test it out it will be part of the inca framework.

UPDATE I found via a comment on this post that theres a solution for this same thing done with a very similar logic and older than mine. You can find it here . I didn't base my logic on that post but what a coincidence. I guess great minds think alike, huh?

package tv.agenciaperu.utils {

import flash.utils.Timer;
import flash.events.TimerEvent;

public class TimerAdv extends Timer {

private var $__initDelay:Number = 0;
private var $__pauseTime:Number = 0;
private var $__acumulado:Number = 0;
private var $__startingTime:Number = NaN;
private var $__paused:Boolean = false;

public function TimerAdv(delay:Number, repeatCount:uint = 0){
$__initDelay = delay;
super(delay, repeatCount);
}

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

override public function start():void{
$__startingTime = new Date().getTime();
super.start();
}

override public function reset():void{
delay = $__initDelay;
$__acumulado = 0;
$__pauseTime = 0;
super.reset();
}

public function pause():void{
super.stop();
$__paused = true;
$__pauseTime = new Date().getTime();
if(isNaN($__startingTime)) $__startingTime = $__pauseTime;
}

public function resume():void{
$__paused = false;
delay = Math.max(0, ($__initDelay - (($__pauseTime - $__startingTime) + $__acumulado)));
$__acumulado += ($__pauseTime - $__startingTime);
addEventListener(TimerEvent.TIMER, restoreInitDelay, false, 0, true);
start();
}

private function restoreInitDelay(e:TimerEvent):void{
removeEventListener(e.type, arguments.callee);
delay = $__initDelay;
$__acumulado = 0;
$__pauseTime = 0;
}

}

}

Let me know if you find a bug plz so i can update the post.

--fernando

Update 1: Fixed a bug in the delay value for future repetitions

7 Comments

Huh? Why do you need this? The TImer class already "pauses" when you call timer.stop(), and resumes when you call timer.start() ... and you can see if it is currently paused or not by checking the "running" property.

I don't understand what this class adds?

Awesome - thanks. Great timing - I was going to write one this week. Saved me a few minutes!

This provides a useful extension to the Timer class. I’d like to encourage you to add this to the new Adobe cookbooks site at http://cookbooks.adobe.com/home in the category “ActionScript”.

If you are new to the cookbook applications, they are a searchable repository for community-generated code samples. I think lots of people would like to utilize this TimerAdv class.

You’ll need an Adobe ID if you don’t already have one. After that, just click “Create a Recipe” on the cookbooks home page. Then, just provide the requested information and code.

Thanks,

Jackie Levy
Technical Writer, API and Developer documentation
Adobe Systems, Inc.

Thanks for sharing! I tried the fjakobs version prior to this and i couldn't get it to work (im a beginner), your class worked straight off though.

About this Entry

This page contains a single entry by fernando published on September 18, 2009 7:31 AM.

Updated: Get current week of the year was the previous entry in this blog.

How to install: flvtool2, flvtool++ and yamdi under Centos is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.