Created a class from setTimeout out of popular demand. Also added some new features like event dispatching and timeout status.
/*
Made by fry at friedcellcollective dot net
Use and distribute this freely. Just include this comment block.
Version 2.0+MX2004
init
usage
init( debug )
parameters
debug If debug is set to true (default) the Timeout objects are not deleted
returns
True if all the methods weren't set before, False if one of the methods were already present on _global
description
Will register setTimeout, getTimeout and clearTimeout on _global, so you don't have to use org.fcc.Timeout all the time.
If you use setTimeout a lot and with huge arguments it's smart to turn debugging off.
Otherwise you will exeperience a memory leak because all the Timeout objects are left untouched.
example
init() // anything that resolves to false
init(1) // anything that resolves to true
setTimeout
usage
setTimeout( function, interval [, arg1, arg2,...,argn] )
setTimeout( object, methodName, interval [, arg1, arg2,...,argn] )
setTimeout( object, method, interval [, arg1, arg2,...,argn] )
parameters
function A reference to a function or an anonymous function object
object An object of type Object or MovieClip
methodName The name of the method to call on the object parameter
method A reference to the method to call on the object parameter
interval The time in milliseconds between now and the call to function or method
arg1, arg2,...,argn Optional parameters to pass to the function or method
returns
A timeout identifier of type Number that you can pass to other methods.
description
Public static method; calls a function or a method after a certain time interval has passed.
Uses the builtin function setInterval.
Usage 3 allows you to call a method of a certain object with a different scope (the object 'this' inside the method)
example
usage 1:
setTimeout(function(){trace('timer called');},1000);
or
function test(arg) {trace('timer '+arg);}
setTimeout(test,1000,'called');
usage 2:
var test:Object = new Object();
test.method = function() {trace('timer called');}
setTimeout(test,"method",1000);
usage 3:
var test:Object = new Object();
test.method = function(arg) {trace('timer '+arg);}
setTimeout(test,test.method,1000,'called');
clearTimeout
usage
clearTimeout( timeoutID )
parameters
timeoutID An id returned from a call to the setTimeout function.
returns
Nothing.
description
Public static method; clears a call to the setTimeout function.
example
var timerID:Number = setTimeout(function(){trace('timer called');},1000);
clearTimeout(timerID);
getTimeout
usage
getTimeout( timeoutID )
parameters
timeoutID An id returned from a call to the setTimeout function.
returns
An object of type org.fcc.Timeout.
description
Public static method; retrieves the Timeout object.
Use this method with care cause it might cause memory leaks even if debugging is turned off.
Use this method to inspect the status of the Timeout object (or to listen to its changes).
Timeout object has the following properties:
obj:Object The scope
fn:Function The function/method to execute
args:Array All arguments passed to the executed function/method
interval:Number Interval in milliseconds
id:Number Id of the Timeout
intid:Number Id of the Interval (returned by the setInterval)
status:String Status of the Timeout; values are "Initializing", "Waiting", "Executed", "Canceled"
returned Value the executed function returns (mixed type)
Timeout object dispatches the following events:
initialized When it enters the state of "Waiting"
executed When it enters the state of "Executed"
canceled When it enters the state of "Canceled"
example
var timerID:Number = setTimeout(function(){trace('timer called');},1000);
var timer:org.fcc.Timeout = getTimeout(timerID);
trace(timer.status); //returns "Waiting"
*/
import mx.events.*;
class org.fcc.Timeout extends Object {
public var _sClassName:String = "org.fcc.Timeout";
public var obj:Object;
public var fn:Function;
public var args:Array;
public var interval:Number;
public var id:Number;
public var intid:Number;
public var status:String;
public var returned;
private function dispatchEvent():Void {};
private function addEventListener():Void {};
private function removeEventListener():Void {};
private function Timeout() {
mx.events.EventDispatcher.initialize(this);
this.obj = null;
this.fn = null;
this.args = null;
this.interval = null;
this.id = null;
this.intid = null;
this.status = "Initializing";
this.returned = null;
}
private function toString():String {
return "[object "+_sClassName+"]";
}
private function valueOf():Number {
return this.id;
}
private static var _bDebug:Boolean = true;
private static var _aTimeouts:Array = new Array();
public static function init(bDebug:Boolean):Boolean {
_bDebug = bDebug;
var _bReturn = true;
if (_global.setTimeout) _bReturn = false;
else _global.setTimeout = setTimeout;
if (_global.getTimeout) _bReturn = false;
else _global.getTimeout = getTimeout;
if (_global.clearTimeout) _bReturn = false;
else _global.clearTimeout = clearTimeout;
return _bReturn;
}
public static function setTimeout():Number {
var oTimer:Timeout = new Timeout();
var nIndex:Number = 0;
if (typeof(arguments[nIndex])=='object' || typeof(arguments[nIndex])=='movieclip') oTimer.obj = arguments[nIndex++];
else oTimer.obj = null;
oTimer.fn = (typeof(arguments[nIndex])!='function')? oTimer.obj[arguments[nIndex]] : arguments[nIndex];
oTimer.interval = parseInt(arguments[++nIndex]);
oTimer.args = arguments.slice(++nIndex);
if (isNaN(oTimer.interval)) return null;
oTimer.id = _aTimeouts.length;
oTimer.intid = setInterval(_execTimeout,oTimer.interval,oTimer.id);
_aTimeouts[oTimer.id] = oTimer;
oTimer.status = "Waiting";
oTimer.dispatchEvent({target:oTimer,type:"initialized"});
return oTimer.id;
}
private static function _execTimeout(id:Number) {
clearInterval(_aTimeouts[id].intid);
var _uReturn = _aTimeouts[id].fn.apply(_aTimeouts[id].obj,_aTimeouts[id].args);
_aTimeouts[id].status = "Executed";
_aTimeouts[id].returned = _uReturn;
_aTimeouts[id].dispatchEvent({target:_aTimeouts[id],type:"executed"});
if (!_bDebug) delete _aTimeouts[id];
return _uReturn;
}
public static function clearTimeout(id:Number):Void {
clearInterval(_aTimeouts[id].intid);
_aTimeouts[id].status = "Canceled";
_aTimeouts[id].dispatchEvent({target:_aTimeouts[id],type:"canceled"});
if (!_bDebug) delete _aTimeouts[id];
}
public static function getTimeout(id:Number):Timeout {
return _aTimeouts[id];
}
}