HTML and CSS Reference
In-Depth Information
The setTimeout() and setInterval() methods are methods of the window object. They
take two parameters:
1. The statements to execute: expressions in quotes or function references.
2. The time in milliseconds to wait before the statements are executed.
Which of the two timer functions should you use? It depends on what you are trying
to do. If the function being called needs different parameters on each call or the content
of the function itself decides whether or not to call itself again, then you would use set-
Timeout() but if the function just needs to be called at regular intervals, then the setInt-
erval() function would produce simpler and faster results.
You can cancel a timeout or interval by calling the clearTimeout() or clearInterval()
method by passing it a reference to the return value of setTimeout() or setInterval() .
FORMAT
var timeout = setTimeout("expression", delaytime);
var timeout= setInterval("expression", intervaltime);
EXAMPLE
var timeout = setTimeout("timer()", 15000); // In 15 seconds call the
// function "timer()"
var timeout = setTimeout(timer,15000); // Use function reference
var timerId = setInterval("scroller()", 500); // Every .5 seconds
// call "scroller()"
var timerId = setInterval(scroller, 500); // Use function reference
To clear the timed event use the clearTimeout() or clearInterval() methods:
clearTimeout(timeout);
clearInterval(timerID);
EXAMPLE 10.10
<html>
<head><title>The setTimeout method</title>
<script type="text/javascript">
1
function changeStatusBar(){
2
window.status = "See me now before I disappear!";
3
timeout =setTimeout("window.status=''", 6000);
// alert(timeout); This value differs in Mozilla and IE
}
</script>
</head>
<body><div align="center">
<font face="arial" size="3" color="blue">
The timeout is set for 6 seconds.
 
Search WWH ::




Custom Search