<title>settimeout example</title>
<script type= "Text/javascript Tutorial" >
function Delayedalert ()
{
Timeoutid = Window.settimeout (Slowalert, 2000);
}
function Slowalert ()
{
Alert ("That is really slow!");
}
function Clearalert ()
{
Window.cleartimeout (Timeoutid);
}
Window.settimeout (' Window.parent.generateOutput () ', 1000);
function Generateoutput (aconcise) {
if (aconcise)
Parent.generateconciseoutput ();
Else
Parent.generateoutput ();
}
Window.settimeout (Generateoutput, 1000, true);
</script>
<body>
<button onclick= "Delayedalert ();"
>show an alert box after 2 seconds</button><br>
<button onclick= "Clearalert ();" >Cancel</button>
<!--
Tips for using settimeout
(1) The call function pass parameter in IE to the settimeout
The above analysis shows that IE is not supported in the settimeout to the called function parameters, for the sake of the harmony of the browser world, we can wrap the function call parameters into the new anonymous function. Example:
function f (a) {
alert (a);
}
settimeout (f,50, ' hello '); For non-IE
settimeout (function () {f (' hello ')},50); General
var str= ' Hello ';
settimeout (function () {f (str)},50); General
(2) This question
The settimeout called function is executed when the context is global, not the context when the SetTimeout method is invoked. So, when the function of the first parameter of settimeout is executed, it points to window, and if you need to keep this when you call the SetTimeout method, then you need to pass this in. Example:
function person (name) {
This.name=name;
var f=function () {alert (' My name is ' +this.name)};
SetTimeout (f,50); Error