« 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
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?
@Jonathan
The stop method doesn't pause the Timer. What stop does is restart the delay and start the remainingCount from where it left.
For example, Timer(10000, 1); if you stop() that at 5000 and then execute the start method again it will start again from 0 until 10000. With pause it will execute the remaining delay and also the remaining count if it's different from 1.
Cheers
Awesome - thanks. Great timing - I was going to write one this week. Saved me a few minutes!
nice post, please have a look at this. http://blog.fjakobs.com/archives/101
Holly! WOW!
I swear i didn't base my solution on your post but the logic it's pretty much the same i have to admit and your post is older than mine so you win. Let me know if you me want to take this post down.
Thanks for letting me know. In the meantime i'm updating the post and linking back to your blog.
And you even fixed the same bug i had on a comment. Damn!
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.