15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

174<br />

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

windowObject.setInterval()<br />

Listing 16-36: setInterval() Demonstration Frameset<br />

<br />

<br />

setInterval() Method<br />

<br />

<br />

<br />

<br />

<br />

<br />

In the top frame is a control panel with several buttons that control the automatic<br />

scrolling of the Bill of Rights text document in the bottom frame. Listing 16-37 shows<br />

the control panel document. Many functions here control the interval, scrolling jump<br />

size, and direction, and they demonstrate several aspects of applying setInterval().<br />

Notice that in the beginning the script establishes a number of global variables.<br />

Three of them are parameters that control the scrolling; the last one is for the ID<br />

value returned by the setInterval() method. The script needs that value to be a<br />

global value so that a separate function can halt the scrolling with the<br />

clearInterval() method.<br />

All scrolling is performed by the autoScroll() function. For the sake of simplicity,<br />

all controlling parameters are global variables. In this application, placement of<br />

those values in global variables helps the page restart autoscrolling with the same<br />

parameters as it had when it last ran.<br />

Listing 16-37: setInterval() Control Panel<br />

<br />

<br />

ScrollBy Controller<br />

<br />

var scrollSpeed = 500<br />

var scrollJump = 1<br />

var scrollDirection = “down”<br />

var intervalID<br />

function autoScroll() {<br />

if (scrollDirection == “down”) {<br />

scrollJump = Math.abs(scrollJump)<br />

} else if (scrollDirection == “up” && scrollJump > 0) {<br />

scrollJump = -scrollJump<br />

}<br />

parent.display.scrollBy(0, scrollJump)<br />

if (parent.display.pageYOffset

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

Saved successfully!

Ooh no, something went wrong!