AS2.0 Event based Timer class.
April 20th, 2006
This Timeout class provides a timer which will dispatch an “onComplete” event message, when the timer is complete.
import mx.utils.Delegate;
import mx.events.EventDispatcher;
class Timeout {
private var timerInterval : Number;
private var duration : Number;
private var destroyed : Boolean = false;
private var running : Boolean = false;
public function addEventListener(type : String, handler : Function) : Void {}
public function removeEventListener(type : String, handler : Function) : Void {}
private function dispatchEvent(e : Object) : Void {}
/**
* Timeout provides a timer which will dispatch an onComplete message.
*
* @usage
* function myTimeoutHandler() {
* trace("Timer complete");
* // do something after timeout?
* }
* var duration : Number = 1; // duration as Seconds
* var myTimeout : Timeout = new Timeout(duration);
* myTimeout.addEventListener("onComplete", myTimeoutHandler);
* myTimeout.go();
*/
public function Timeout(duration : Number) {
EventDispatcher.initialize(this);
if(duration != undefined) {
this.duration = Math.abs(duration)*1000;
}
}
public function go() : Void {
if(!running) {
running = true;
setTimeout();
} else {
restart();
}
}
private function setTimeout () : Void {
timerInterval = setInterval (Delegate.create(this, timeoutComplete), duration);
}
public function restart (newDuration : Number) : Void {
if(newDuration != undefined) {
duration = newDuration;
}
dispose();
go();
}
public function dispose () : Void {
clearInterval(timerInterval);
running = false;
}
public var crush : Function = dispose;
public var kill : Function = dispose;
public var destroy : Function = dispose;
public function toString() : String {
return "[Object Timer : "+ timerInterval + " ]";
}
[Event("onComplete")]
private function timeoutComplete ():Void {
clearInterval(timerInterval);
dispatchEvent({type:"onComplete", target:this});
}
}
Usage
function myTimeoutHandler() {
trace("Timer complete");
// do something after timeout?
}
var duration : Number = 1; // duration as Seconds
var myTimeout : Timeout = new Timeout(duration);
myTimeout.addEventListener("onComplete", myTimeoutHandler);
myTimeout.go();
If you’d like to stop the Timer before it’s completed…
myTimeout.dispose();. Or you can use myTimeout.crush();, myTimeout.kill();, myTimeout.destroy(); depending on your mood.
You can restart the timer myTimeout.restart(); and change the timer duration myTimeout.restart(2);
can you Digg it?
is it del.icio.us? 
Category: General



7 Comments Add your own
1. Epictive | April 20th, 2006 at 6:45 pm
You know that setTimeout is actually an undocumented AS function? You can actually use it like you would in JS like:
function calledOnlyOnce(Void):Void {
trace(’I was called once’);
}
setTimeout(this, ‘calledOnlyOnce’, 1000);
2. Administrator | April 21st, 2006 at 10:16 am
Thing is, setTimeout is an undocumented Flash 8 function… that’s why I didn’t use it in the class, many projects are still targeted at Flash 6 (sadly!)
So this is for maximum compatibility in AS2.0.
3. Epictive | April 21st, 2006 at 6:27 pm
It is working in my movie published as Flash 6 fine. Is it not for you?
4. Administrator | April 25th, 2006 at 12:27 pm
Yep, it is working when I compile from Flash 8 to a FlashPlayer 6 profile. This wasn’t the problem I was trying to solve here.
The thing is many people have yet to upgrade to Flash 8, (sad as that may be.) And so the class uses setInterval instead of setTimeout.
5. Administrator | April 25th, 2006 at 12:46 pm
Of course this is also an example of the Observer & Chain of Responsibility GOF design patterns, if you have the option to use the TopLevel.setTimeout() function I would still place it within the event framework used in this class.
If you need to do something quick and dirty then it’s always ok to use whatever method you like, however, when you develop larger systems in Flash using AS2.0 it is (very often) beneficial in the long run to design your application along the lines of an event based model.
As an aside, I’ve been looking closely at the NSNotificationCenter which has been implemented in the ASAPFramework and also in ActionStep, this is a very cool event model which allows objects to listen for messages for which they don’t neccessarily know the broadcaster.
Some more info about Notifications.
6. ketan | April 28th, 2006 at 1:35 am
Potential bug if somethings calls Timeout.go() before the timer is has run out.
Simple fix in the setTimeout Function to add clearInterval
private function setTimeout () : Void {
clearInterval(timerInterval);
timerInterval = setInterval (Delegate.create(this, timeoutComplete), duration);
}
7. Jason Milkins | April 28th, 2006 at 9:28 am
go()already handles this using therunningflag.Look more closely at these methods..
// --- >8 snip public function go() : Void { if(!running) { running = true; setTimeout(); } else { restart(); } } private function setTimeout () : Void { timerInterval = setInterval (Delegate.create(this, timeoutComplete), duration); } public function restart (newDuration : Number) : Void { if(newDuration != undefined) { duration = newDuration; } dispose(); go(); } // --- >8 snipgo()doesn’t runsetTimeout()unconditionally, alsosetTimeoutis a private method. The public methods are the constructor,go(),restart()anddispose()(and it’s aliases, crush, kill, destroy ;-) )I hope this addresses your potential bug, if I’m on the wrong track, could you explain the circumstances under which the bug could arise. Cheers.
Leave a Comment
You must be logged in to post a comment .
Trackback this post | Subscribe to the comments via RSS Feed