Timeouts don't actually make the browser stop what it's doing. Although the statement you specify in the setTimeout method won't be executed until the delay passes, the browser will continue to do other things while it waits (for example, acting on event handlers).
A variable (ident in this example) stores an identifier for the timeout. This enables you to set multiple timeouts, each with its own identifier. Before a timeout has elapsed, you can stop it with the clearTimeout() method, specifying the identifier of the timeout to stop:
Updating a Page with Timeouts
Normally, a timeout only happens once because the statement you specify in the setTimeout statement is only executed once. But often, you'll want your statement to execute over and over. For example, your script may be updating a clock or countdown and need to execute once per second.
You can make a timeout repeat by issuing the setTimeout() method call again in the function called by the timeout. Listing 11.3 shows an HTML document that demonstrates a repeating timeout.
Listing 11.3 Using timeouts to update a page every two seconds
This program displays a message in the status line and in a text field every two seconds, including a counter that increments each time. You can use the Reset button to start the count over and the Stop button to stop the counting.
This script calls the setTimeout() method when the page loads, and again at each update. The Update() function performs the update, adding one to the counter and setting the next timeout. The Reset button sets the counter to zero, and the Stop button demonstrates the clearTimeout() method. Figure 11.2 shows Internet Explorer's display of the timeout example after the counter has been running for a while.
Figure 11.2. The output of the timeout example.