setTimeout Function
setTimeout() : Number
Player version: Flash Player 8
Runs a specified function after a specified delay (in milliseconds). The setTimeout() function is similar to the setInterval() function, except that setTimeout() calls the function once and then is automatically deleted.
To preserve the ability to use the clearTimeout() method to prevent setTimeout() from calling the function, be sure to assign the return value of the setTimeout() call to a variable.
Parameters
functionReference:Object — The name of the function to execute. Do not include quotation marks or parentheses, and do not specify parameters of the function to call. For example, use functionName, not functionName() or functionName(param).
delay:Number — The delay, in milliseconds, until the function is executed.
args:Object — Zero or more arguments, separated by commas, to be passed to the function.
Returns
Number — Unique numeric identifier for the timed process.
Example
The following example uses setTimeout() to call a function named my_delayedFunction after a two(2) second delay, and uses the return value to call clearTimeout() if the user presses the Escape key. The example will output the string "two second delay" after two seconds has elapsed, unless the user presses the Escape key before my_delayedFunction is called.
var my_timedProcess:Number = setTimeout(my_delayedFunction, 2000, "two second delay");
function my_delayedFunction (arg1) {
trace(arg1);
}
var escListener:Object = new Object();
escListener.onKeyDown = function() {
if (Key.isDown(Key.ESCAPE)) {
clearTimeout(my_timedProcess);
}
};
Key.addListener(escListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
Partager