Java Reference
In-Depth Information
Notice that this time you have two additional methods, getUTCDay() and toISOString() . The
getUTCDay() method works in the same way as the getDay() method and returns the day of the
week as a number, from 0 for Sunday to 6 for Saturday. Because the day of the week is decided by
the day of the month, the month, and the year, there is no setUTCDay() method.
The toISOString() method is relatively new to JavaScript, and it returns the date and time in an
ISO‐formatted string. The format is:
YYYY-MM-DDTHH:mm:ss.sssZ
The ISO format separates the date from the time with the literal character T . So YYYY‐MM‐DD is the
date, and HH:mm:ss.sss is the time. The Z at the end denotes the UTC time zone. The ISO formatted
string for March 30, 2014 at 3:10 PM UTC is:
2014-03-30T15:10:00Z
When you learn about forms in Chapter 11, you'll revisit dates and times to build a time converter.
timers in a WeB paGe
You can create two types of timers: one‐shot timers and continually firing timers. The one‐shot
timer triggers just once after a certain period of time, and the second type of timer continually
triggers at set intervals. You investigate each of these types of timers in the next two sections.
Within reasonable limits, you can have as many timers as you want and can set them going at
any point in your code, such as when the user clicks a button. Common uses for timers include
animating elements, creating advertisement banner pictures that change at regular intervals, and
displaying the changing time in a web page.
one‐shot timer
Setting a one‐shot timer is very easy; you just use the setTimeout() function:
var timerId = setTimeout(yourFunction, millisecondsDelay)
The setTimeout() method takes two parameters. The first is a function you want executed, and the
second is the delay, in milliseconds (thousandths of a second), until the code is executed.
The method returns a value (an integer), which is the timer's unique ID. If you decide later that you
want to stop the timer firing, you use this ID to tell JavaScript which timer you are referring to.
Delaying a Message
trY it out
In this example, you set a timer that fires three seconds after the page has loaded:
<!DOCTYPE html>
<html lang="en">
<head>
 
Search WWH ::




Custom Search