15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

176<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

The setInterval() method is invoked inside the startScroll() function.<br />

This function initially “burps” the page by one scrollJump interval so that the test<br />

in autoScroll() for the page being scrolled all the way to the top doesn’t halt a<br />

page from scrolling before it gets started. Notice, too, that the function checks for<br />

the existence of an interval ID. If one is there, it is cleared before the new one is set.<br />

This is crucial within the design of the example page, because repeated clicking of<br />

the Start Scrolling button triggers multiple interval timers inside the browser. Only<br />

the most recent one’s ID would be stored in intervalID, allowing no way to clear<br />

the older ones. But this little side trip makes sure that only one interval timer is<br />

running. One of the global variables, scrollSpeed, is used to fill the delay parameter<br />

for setInterval(). To change this value on the fly, the script must stop the<br />

current interval process, change the scrollSpeed value, and start a new process.<br />

The intensely repetitive nature of this application is nicely handled by the<br />

setInterval() method.<br />

setTimeout(“expr”, msecDelay [, language])<br />

setTimeout(functionRef, msecDelay [,<br />

funcarg1, ..., funcargn])<br />

windowObject.setTimeout()<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

When you load the HTML page in Listing 16-38, it triggers the updateTime()<br />

function, which displays the time (in hh:mm am/pm format) in the statusbar.<br />

Instead of showing the seconds incrementing one by one (which may be distracting<br />

to someone trying to read the page), this function alternates the last character of<br />

the display between an asterisk and nothing, like a visual “heartbeat.”<br />

Listing 16-38: Display the Current Time<br />

<br />

<br />

Status Bar Clock<br />

<br />

12) ? theHour - 12 : theHour)<br />

theTime += ((theMin < 10) ? “:0” : “:”) + theMin

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!