# setTimout

**URL:** <https://forum.kirupa.com/t/settimout/146157>\
**Category:** Uncategorized\
**Created:** [April 28, 2005, 5:33pm UTC](https://forum.kirupa.com/t/settimout/146157 "2005-04-28T17:33:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![claudio](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@claudio](https://forum.kirupa.com/u/claudio)\
**Post date:** [April 28, 2005, 5:33pm UTC](https://forum.kirupa.com/t/settimout/146157/1 "2005-04-28T17:33:59Z")

</div>

It’s basically a setInterval with no worries of cleaning the interval, as it executes the function or instruction only once.

```auto
//	Executes a function or instruction after a
//	number of milisseconds and then clears the call.
_global.setTimeout = function() {
	if (arguments[0] instanceof Function) {
		this.obj = null;
		this.method = arguments[0];
		this.interval = arguments[1];
		this.args = arguments[2] instanceof Array ? arguments[2] : [arguments[2]];
	} else if (typeof (arguments[0]) == "object" || typeof (arguments[0]) == "movieclip") {
		this.obj = arguments[0];
		this.method = eval(arguments[1]);
		this.interval = arguments[2];
		this.args = arguments[3] instanceof Array ? arguments[3] : [arguments[3]];
	}
	this.$function = function() {
		this.method.apply(this.obj, this.args);
		clearInterval(this.$interval);
	};
	this.$interval = setInterval(this, "$function", this.interval);
};
setTimeout.prototype.clearTimeout = function() {
	clearInterval(this.interval);
};
//	Usage is similar to setInterval, except that if more than
//	one parameter is passed, it must be passed as an Array.
//
//	setTimeout(functionName,interval [,param1,param2,...,paramN])
// or
//	setTimeout(obj,"functionName",interval [,param1,param2,...,paramN])
//
//	Examples:
var name = "claudio";
function Test(msg) {
	trace("My name is "+msg);
}
function Sum(a, b) {
	trace("Result = "+(a+b));
}
//	executes the Scream function after 3000 milisseconds passing the variable "name" as a parameter
var a = new setTimeout(Test, 2000, name);
//	tell the movieclip square to gotoAndStop frame "end" after 4000 milisseconds
var b = new setTimeout(square, "gotoAndStop", 3000, "fim");
//	cancel the timeout call above
b.clearTimeout();
//	executes the Sum function after 4000 milisseconds passing the values "10" and "5" as parameters
var b = new setTimeout(Sum, 4000, [10, 5]);

```
