Java Reference
In-Depth Information
The changes you made were short and simple. In the function window_onload(), you have added a
timer that will call the updateTime() function every 1,000 milliseconds — that is, every second. It'll
keep doing this until you leave the page. Previously your updateTime() function was called only
when the user clicked either a different city in the list box or the summertime check box.
The window_onload() function is connected to the window object's onload event in the <body> tag,
so after the page has loaded your clock starts running.
That completes your look at this example and also your introduction to timers.
Summary
You started the chapter by looking at Coordinated Universal Time (UTC), which is an international stan-
dard time. You then looked at how to create timers in web pages.
The particular points covered were the following:
The
Date object enables you 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 get-
ting 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. One pitfall of this
is that you 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, you found you could start a timer going that would fi re just
once after a certain number of milliseconds. setTimeout() takes two parameters: the fi rst is the
code you want executed, and the second is the delay before that code is executed. It returns a
value, the unique timer ID that you can use if you later want to reference the timer; for example,
to stop it before it fi res, you use the clearTimeout() method.
To create a timer that fi res at regular intervals, you used the
setInterval() method, which
works in the same way as setTimeout() , except that it keeps fi ring unless the user leaves the
page or you call the clearInterval() method.
In the next chapter, you'll be looking at a way of storing information on the user's computer using
something called a cookie. Although they may not be powerful enough to hold a user's life history,
they are certainly enough for us to keep track of a user's visits to the website and what pages they view
when they visit. With that information, you can provide a more customized experience for the user.
Search WWH ::




Custom Search