How the Book of JavaScript Website's Timer Works

The timer on the Book of JavaScript website works similarly to the clock, but with a few twists added. Figure 9-9 shows the code.

Code to show how long you've been on a page

Figure 9-9. Code to show how long you've been on a page

The fun starts with the onLoad in the <body> tag in ❿. This onLoad calls the function setTimeSinceArriving() and passes it two parameters: 0, which indicates that this is the first time the function is being called, and 500, the number of milliseconds to pass to the setTimeout() function—in other words, the time between calls to the setTimeSinceArriving() function.

The setTimeSinceArriving() function is defined at the ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.