Building a Clock with Timing Loops

Clocks are one obvious application of timing loops. Figure 9-7 provides the code for a simple clock, and Figure 9-8 shows the resulting page in a browser. See if you can understand what's going on before you read the analysis. Start by looking at the form in the body of the page.

Code for a JavaScript clock

Figure 9-7. Code for a JavaScript clock

The JavaScript clock running in Firefox

Figure 9-8. The JavaScript clock running in Firefox

Line-by-Line Analysis of Figure 9-7

The heart of Figure 9-7's script is the writeTime() function. Every second, this function figures out the current ...

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.