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.

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

by, the bars for hours, minutes, and seconds adjust their widths to reflect the current<br />

time. At the same time, the innerHTML of SPAN elements to the right of each<br />

bar display the current numeric value for the bar.<br />

The dynamically calculated values in this example are based on the creation of<br />

a new date object over and over again to get the current time from the client computer<br />

clock. It is from the date object (stored in the variable called now) that the<br />

hour, minute, and second values are retrieved. Some other calculations are<br />

involved so that a value for one of these time components is converted into a<br />

pixel value for the width of the bars. The bars are divided into 24 (for the hours)<br />

and 60 (for the minutes and seconds) parts, so the scale for the two types differs.<br />

For the 60-increment bars in this application, each increment is set to 5 pixels<br />

(stored in shortWidth); the 24-increment bars are 2.5 times the shortWidth.<br />

As the document loads, the three SPAN elements for the colored bars are given<br />

no width, which means that they assume the default width of zero. But after the<br />

page loads, the onLoad event handler invokes the init() function, which sets the<br />

initial values for each bar’s width and the text (innerHTML) of the three labeled<br />

spans. Once these initial values are set, the init() function invokes the<br />

updateClock() function.<br />

In the updateClock() function, a new date object is created for the current<br />

instant. The document.recalc() method is called, instructing the browser to<br />

recalculate the expressions that were set in the init() function and assign the new<br />

values to the properties. To keep the clock “ticking,” the setTimeout() method is<br />

set to invoke this same updateClock() function in one second.<br />

To see what the getExpression() method does, you can click the button on the<br />

page. It simply displays the returned value for one of the attributes that you assign<br />

using setExpression().<br />

Listing 15-32: Dynamic Properties<br />

<br />

<br />

getExpression(), setExpression(), and recalc() Methods<br />

<br />

TH {text-align:right}<br />

SPAN {vertical-align:bottom}<br />

<br />

<br />

var now = new Date()<br />

var shortWidth = 5<br />

var multiple = 2.5<br />

function init() {<br />

with (document.all) {<br />

hoursBlock.style.setExpression(“width”,<br />

“now.getHours() * shortWidth * multiple”,”jscript”)<br />

hoursLabel.setExpression(“innerHTML”,<br />

“now.getHours()”,”jscript”)<br />

minutesBlock.style.setExpression(“width”,<br />

“now.getMinutes() * shortWidth”,”jscript”)<br />

elementObject.setExpression()

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

Saved successfully!

Ooh no, something went wrong!