setTimeout Method

DHTML, HTML, & CSS

setTimeout Method


Evaluates an expression after a specified number of milliseconds has elapsed.

Syntax

iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])

Parameters

vCodeRequired. Variant that specifies the function pointer or string that indicates the code to be executed when the specified interval has elapsed.
iMilliSecondsRequired. Integer that specifies the number of milliseconds.
sLanguageOptional. String that specifies any one of the possible values for the LANGUAGE attribute.

Return Value

Integer. Returns an identifier that cancels the evaluation with the clearTimeout method.

Remarks

In versions earlier than Microsoft® Internet Explorer 5, the first argument of setTimeout must be a string. Evaluation of the string is deferred until the specified interval elapses.

As of Internet Explorer 5, the first argument of setTimeout can be a string or a function pointer.

The specified expression or function is evaluated once. For repeated evaluation, use the setInterval method.

Example

The following examples use the setTimeout method to evaluate a simple expression after 1 second has elapsed.

Sample Code

This example uses the setTimeout method to evaluate a slightly more complex expression than the one in the preceding example after 1 second has elapsed.

window.setTimeout("alert('Hello, world')", 1000);

The following example uses the setTimeout method to evaluate a slightly more complex expression after 1 second has elapsed.

var sMsg = "Hello, world";
window.setTimeout("alert(" + sMsg + ")", 1000);

This example uses the setTimeout method to hide a button object after three seconds. If the user clicks the Count Down button and then counts to three, the Now You See Me button disappears.

<SCRIPT>
function fnHide(oToHide){
   window.setTimeout("fnHide2(" + oToHide.id + ")", 3000);
}
function fnHide2(sID){
   var o = eval(sID);
   o.style.display="none";
}
</SCRIPT>
<INPUT TYPE=button VALUE="Now you see me ..." 
    ID="oHideButton" onclick="fnHide(this)">

In the preceding example, the ID of the button is passed as a parameter to the function invoked by the setTimeout method. This is possible only when a string is passed as the first argument.

This example uses a function pointer to pass the data. In this case, the data is stored in a global variable because it cannot be passed directly.

<SCRIPT>
var g_oToHide = null;

function fnHide(oToHide){
    g_oToHide = oToHide;
    window.setTimeout(fnHide2, 3000);
}
function fnHide2(sID){
    if (g_oToHide) {
       g_oToHide.style.display="none";
    }
}
</SCRIPT>
<INPUT TYPE=button VALUE="Now you see me ..." ID="oHideButton" 
    onclick="fnHide(this)">
This feature requires Microsoft® Internet Explorer 3 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft 
	 Internet
	 Explorer

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
Version data is listed when the mouse hovers over a link, or the link has focus.

window


Back to topBack to top

Did you find this topic useful? Suggestions for other topics? write us!Internet Link

© 1999 microsoft corporation. all rights reserved. terms of useInternet Link.