HTML and CSS Reference
In-Depth Information
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
Timing Methods
What They Do
setTimeout()
Invokes a function or evaluates an expression or a function when the
number of milliseconds has passed.
setInterval()
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
JavaScript sees time in terms of milliseconds, Table 10.9 gives you a little conversion
table to help determine the time in milliseconds.
Table 10.9 Basic Units of Time
Unit of Time
Milliseconds
1 second
1,000
1 minute
second * 60
(1,000 * 60)
1 hour
minute * 60
(1,000 * 60 * 60)
1 day
hour * 24
(1,000 * 60 * 60 * 24)
1 week
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
happen in the future For a detailed explanation of how JavaScript internally handles the
timers, see http://www.howtocreate.co.uk/tutorials/JavaScript/timers.
3. For an example of a timer to create animation, see Chapter 15, “The W3C DOM and JavaScript.”
 
Search WWH ::




Custom Search