SetTimeout Method
Evaluates an expression after a specified number of milliseconds have elapsed. Syntax var retval = window.settimeout (expression, msec, language); Parameters expression [in]
Type: Variant
Variant that specifies the function pointer or a string that indicates the code is executed when the specified I Nterval has elapsed. msec [in]
Type: Integer
The Integer that specifies the number of milliseconds. language [in, optional]
Type: Variant
String that specifies one of the following values:
JScript
Language is JScript.
VBScript
Language is VBScript.
JavaScript
Language is JavaScript. Return Value
Type: Integer
Integer. Returns An identifier this cancels the evaluation with the cleartimeout method. Standards Information
There is 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 is a Func tion pointer to call a function within the HTML Component (HTC) file or a string to call a function in the primary documen T.
Note In Windows Internet Explorer, you cannot pass arguments to the callback function directly; However, can simulate passing arguments by creating a anonymous closure function that references variables within SCO PE of the call to setinterval or setTimeout. For more information, see setinterval.
The first argument of SetTimeout can be a string or a function pointer. Examples
The following example uses the SetTimeout method to evaluate a simple expression after one second have elapsed. Javascript
Window.settimeout ("alert (' Hello, World ')", 1000);
The following example uses the SetTimeout method to evaluate a slightly more complex expression after one second has elapsed. Javascript
var sMsg = "Hello, world";
Window.settimeout ("alert (" + 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" see Me button disappears. Javascript
<script type= "Text/javascript" >
function fnhide (otohide) {
window.settimeout ("FnHide2 (" + Otohide.id + ("),");
}
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. The data is stored in a global variable because it cannot be passed directly. In the preceding example, the ID of the button was passed as a parameter to the function invoked by the Settim Eout method. This was possible only if a string is passed as the first argument.
Code Example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/setTimeout.htm Javascript
<script type= "Text/javascript" >
var g_otohide = null;
function Fnhide (otohide) {
g_otohide = otohide;
Window.settimeout (FnHide2);
}
function FnHide2 (SID) {
if (g_otohide) {
g_otohide.style.display= "none";
}
}
</script>
<input type= "button" value= "Now, see me ..."
id= "Ohidebutton" onclick= "fnhide (This)" >
See also
window
cleartimeout
Requestanimationframe
setimmediate
setinterval
SetInterval Method
Evaluates an expression each time a specified number of milliseconds have elapsed. Syntax var retval = window.setinterval (expression, msec, language); Parameters expression [in]
Type: Variant
Variant that specifies a function pointer or string This indicates the code to being executed when the specified int Erval has elapsed. msec [in]
Type: Integer
The Integer that specifies the number of milliseconds. language [in, optional]
Type: Variant
String that specifies any one of the possible values for the LANGUAGE attribute. Return Value
Type: Integer
Integer. Returns An identifier this cancels the timer with the clearinterval method. Standards Information
There is no standards that apply here. Remarks
The setinterval method continuously evaluates the specified expression until the timer is removed with the cl Earinterval method.
To pass a function as a string, being sure to append the function name with parentheses.
Window.setinterval ("SomeFunction ()", 5000);
When passing a function pointer, does not include the parentheses.
Window.setinterval (someFunction, 5000);
When you use the setinterval method with Introduction to DHTML behaviors, the value of expression should is a fun Ction pointer to call a function within the HTML Component (HTC) file or a string to call a function in the primary docume Nt.
Note In Windows Internet Explorer, you cannot pass arguments to the callback function directly; However, can simulate passing arguments by creating a anonymous closure function that references variables within SCO PE of the call to setinterval or setTimeout. For more information, see Examples.
In versions earlier than Microsoft Internet Explorer 5, the first argument of SetInterval must is a string. Evaluation of the string is deferred until the specified interval elapses.
As of Internet Explorer 5, the first argument of setinterval can be passed as a string or as a function pointer. Examples
This example uses the setinterval method to create a DHTML clock. A variable is assigned to the interval, and can being used as a reference to stop the interval by using the clearinterval method.
Code Example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/setInterval.htm Javascript
var ointerval = "";
function Fnstartclock () {
ointerval = setinterval (fndoclock,200);
}
function Fndoclock () {
//Code to display hours, minutes, and seconds.
}
Window.onload = Fnstartclock;
The next example demonstrates how to pass arguments to a function with setTimeout or setinterval. To does this, create a inner anonymous function to wrap the real callback function. In the new function scope, you can refer to variables declared prior to the call to SetTimeout (such as Div). This structure are referred to as a "closure" in JScript
Code Example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/setInterval2.htm Javascript
//The first example of a closure passes the variable to a named function. function Starttimer () {var div = docu
Ment.getelementbyid (' currenttime ');
SetTimeout (function () {doclock (div)},200);
}//The second example also uses a closure, by referring to an argument passed to the function.
function Doclock (obj) {setinterval (function () {obj.innerhtml= (New Date ()). toLocaleString ()},200);}