Repeating Timed Actions

You've already seen examples of repeating timed actions. The tip box discussed in the section "How the Book of JavaScript Tip Box Works" on page 136 displays each tip for 3.5 seconds, and the text box that tells you how long you've been on the page updates every second. Both of these examples use the same mechanism.

As you have just seen in Chapter 8, the usual way to get JavaScript to perform an action repeatedly is to put the action into a loop. Unfortunately, we can't use a while or for loop for repeated timed events. The while and for loops run too quickly, and there's no good way to slow them down. Even if you could slow them, there's no way to time these loops accurately, because they run at different speeds on different ...

Get The Book of JavaScript, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.