Flash MX | setTimeout, clearTimeout

Needed a setTimeout for Flash MX and needed one just like this. Saw some other solutions but they just didn't feel right. Most probably there is one just like this somewhere and probably some better ones.

Links

Code

/* 
Made by fry at friedcellcollective dot net
Use and distribute this freely. Just include this comment block.
Version 1.0

setTimeout:public
	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 that you can pass to clearTimeout to cancel the timer.
	description
		Action; 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:
			test = new Object();
			test.method = function() {trace('timer called');}
			setTimeout(test,"method",1000);
		usage 3:
			test = new Object();
			test.method = function(arg) {trace('timer '+arg);}
			setTimeout(test,test.method,1000,'called');
clearTimeout:public
	usage
		clearTimeout( timeoutID )
	parameters
		timeoutID	An id returned from a call to the setTimeout function.
	returns
		nothing.
	description
		Action; clears a call to the setTimeout function.
	example
		timerID = setTimeout(function(){trace('timer called');},1000);
		clearTimeout(timerID);
_timeoutArray:private
_execTimeout:private
	usage
		_execTimeout( timeoutID )
	parameters
		timeoutID	An id returned from a call to the setTimeout function.
	returns
		Whatever the called function or method returns.
	description
		Action; calls the set function or method and clears the timeout.
	example
		timerID = setTimeout(function(){trace('timer called');},1000);
		_execTimeout(timerID);
*/
_global._timeoutArray = new Array();
_global.setTimeout = function() {
	var timer = new Object();
	var interval = 0;
	var s = 0;
	if (typeof(arguments[s])=='object' || typeof(arguments[0])=='movieclip') timer.obj = arguments[s++];
	else timer.obj = null;
	timer.fn = (typeof(arguments[s])!='function')? timer.obj[arguments[s]] : arguments[s];
	interval = parseInt(arguments[s+1]);
	timer.args = arguments.slice(s+2);
	if (typeof(timer.fn)!='function' || isNaN(interval)) return null;
	timer.id = _timeoutArray.length;
	timer.intid = setInterval(_execTimeout,interval,timer.id);
	_timeoutArray[timer.id] = timer;
	return timer.id;
}
_global._execTimeout = function(id) {
	var timer = _timeoutArray[id];
	clearInterval(timer.intid);
	return timer.fn.apply(timer.obj,timer.args);
}
_global.clearTimeout = function(id) {
	clearInterval(_timeoutArray[id].intid);
}
ASSetPropFlags(_global,"_timeoutArray,setTimeout,_execTimeout,clearTimeout",3);

Valid XHTML 1.1! Valid CSS!