Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

setTimeout method

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

Syntax

var retval = window.setTimeout(expression, msec);

Parameters

expression [in]

Type: Variant

Variant that specifies the function pointer or string that indicates the code to be executed when the specified interval has elapsed.

msec [in]

Type: Integer

Integer that specifies the number of milliseconds.

Return value

Type: Integer

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

Standards information

There are no standards that apply here.

Remarks

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

When you use the setTimeout method with Introduction to DHTML Behaviors, the value of expression should be a function pointer to call a function within the HTML Component (HTC) file or a string to call a function in the primary document.

Examples

The following example uses the setTimeout method to evaluate a simple expression after one second has elapsed.


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

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


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

This example uses the setTimeout method to hide a input type=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 type="text/javascript">
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="Count Down" 
    id="oHideButton" onclick="fnHide(this)">

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. 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.


<script type="text/javascript">
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)">

See also

window
clearTimeout
requestAnimationFrame
setImmediate
setInterval

 

 

Show:
© 2014 Microsoft