View comments | RSS feed

setTimeout function

setTimeout() : Number

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.

Availability: ActionScript 1.0; Flash Player 8

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.

See also

clearTimeout function, setInterval function


Flash CS3


Comments


glance.ca said on Aug 28, 2007 at 11:14 AM :
I found this tip on http://www.flashguru.co.uk/flash-8-settimeout
"Note: That to use the setTimeout function in Actionscript 2 classes, you will
need to use array notation to avoid compiler errors as this function was left
out of the intrinsic class definition files:"
//call the showPopup function once after a second (1000 ms)
_global['setTimeout'](this,'showPopup',1000);
ufitzi said on Oct 5, 2007 at 7:36 AM :
Thanks!
That saved me TONS of time.
No screen name said on Feb 28, 2008 at 10:46 PM :
for some reason I am getting the error,

1180: Call to a possibly undefined method setTimeout.
punchkickinteractive said on Apr 18, 2008 at 5:20 AM :
Thank you for the _global['setTimeout'](this,'theFunction',2000); tip. Does
anyone know what import statement could be used instead?

For instance, I use the following import statement to access the
Dispatcher functions:
import mx.events.EventDispatcher;


-Ryan

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001218.html