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() .
var timeout = setTimeout("expression", delaytime);
var timeout= setInterval("expression", intervaltime);
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:
<head><title>The setTimeout method</title>
<script type="text/javascript">
function changeStatusBar(){
window.status = "See me now before I disappear!";
timeout =setTimeout("window.status=''", 6000);
// alert(timeout); This value differs in Mozilla and IE
<body><div align="center">
<font face="arial" size="3" color="blue">
The timeout is set for 6 seconds.
Search WWH ::

Custom Search