setTimeout() Examples
This web page gives an example of how to use the setTimeout() function.
Notes
- Does not stop the script from executing. Therefore this function
does not act like a pause.
- setTimeout( "FunctionorExpression", msecDelay ) - There is other
parameters but IE & NS are different.
Ex: setTimeout( "fMyFunc1()", (3*1000) ) - Will call
fMyFunc1() in 3 seconds.
- Passing Parameters - Don't use parameters, use global vars because IE & NS
handle differently. (see examples below).
- setTimeout Parameters - IE vs Netscape - The first 2 parameters of this function are identical for IE
& NS. The following parameters have different meanings so here is your
WARNING.
Basic Use of setTimeout
- Click here to execute the function
fMyFunc1().
- Click here to execute the
function
fCallMyFunc1() which will call fMyFunc1() after 3 seconds.
Using setTimeout to close a window.
- Click Here to
launch a new window that will close it's self using the setTimeout().
Passing Parameters to functions called by setTimeout()
I suggest to place what would normally be a parameter for the functions
into global var's. Set the var prior to the setTimeout() function and
then have the called function use them. Not to clean but it's
necessary to be compatible with IE & NS.
Examples
- Note: gstrName = "No Name" as a default.
- Step1 Click here to execute the function
fMyFunc2() which will display "Hello" + gstrName.
- Step 2 Click here to
execute the function fCallMyFunc2() which will set the variable gstrName to
"John" and then
call fMyFunc2() after 2 second delay.
- Repeat Step 1 again - Notice the value of the global variable gstrName
- Step 3 Click here to
execute the function fCallMyFunc2() which will set the variable gstrName to
"Susie" and then
call fMyFunc2() after 2 second delay.
- Repeat Step 1 again - Notice the value of the global variable gstrName
Using Multiple setTimeout() functions
- Click here - then watch the status bar. Every second a new function
should be called from A - C. At the end an alert box will popup.