HTML and CSS Reference
setTimeout() and setInterval() (see Table 10.8). The setTimeout() method evaluates an
expression after a specified amount of time and quits. The setInterval() method automat-
ically reschedules the execution of an expression at set intervals, continuing to repeat
until the program ends or the setInterval is cancelled.
Table 10.8 Timers
What They Do
Invokes a function or evaluates an expression or a function when the
number of milliseconds has passed.
Invokes a function or evaluates an expression or a function at set
intervals in milliseconds.
Both methods have the same syntax, two arguments: a quoted expression or function
reference, and the time in milliseconds to delay execution of the expression. (A minute
contains 60,000 milliseconds, so 30 seconds would be 30,000 milliseconds.) Because
table to help determine the time in milliseconds.
Table 10.9 Basic Units of Time
Unit of Time
second * 60
(1,000 * 60)
minute * 60
(1,000 * 60 * 60)
hour * 24
(1,000 * 60 * 60 * 24)
day * 7
(1,000 * 60 * 60 * 24 * 7)
If a function contains a setTimeout() or setInterval() method that in short intervals
keeps invoking the function, the result can give the effect of continuous motion such as
a scrolling panorama or message, or even animation. 3 Often, timers are used to scroll
messages in the title or status bars or in containers within a window such as a div or
textbox. You must decide what is tasteful on your Web page and what is annoying, but
that aside, we use setTimeout() and clearTimeout() methods for scheduling something to