We started the chapter by looking at Universal Time Coordination (UTC time), which is an international standard time. We then looked at how to create timers in web pages.
The Date object allows us to set and get UTC time in a way similar to setting a Date object's local time by using methods (such as setUTCHours() and getUTCHours()) for setting and getting UTC hours with similar methods for months, years, minutes, seconds, and so on.
A useful tool in international time conversion is the getTimezoneOffset() method, which returns the difference, in minutes, between the user's local time and UTC time. One pitfall of this is that we are assuming the user has correctly set his time zone on his computer. If not, getTimezoneOffset() is rendered useless as will be any local date and time methods if the user's clock is incorrectly set.
Using the setTimeout() method, we found we could start a timer going that would fire just once after a certain number of milliseconds. setTimeout() takes two parameters: the first is the code we want executed, and the second is the delay before that code is executed. It returns a value, the unique timer ID that we can use if we later want to reference the timer; for example, to stop it before it fires, we use the clearTimeout() method.
To create a timer that fires at regular intervals, we used the setInterval() method, which works in the same way as setTimeout(), except that it keeps firing unless the user leaves the page or we call the clearInterval() method.
Finally, using our new knowledge of timers, we changed the trivia quiz so that the user can determine how many questions she wants to answer and within what time limit the quiz must be completed.
In the next chapter we'll be looking at the top seven mistakes of all time, ones everyone makes at some point regardless of how much of an expert he or she may claim to be. We'll also be looking at a script debugger for Internet Explorer and how it can be used to step through live code, line by line. Finally, we'll be looking at how to avoid errors and how to deal with errors when we get them.