Your Ad Here

Monday, February 25, 2008

calculate date &diff


When the page first loads, it assigns the current (this moment) date/time value to the variable Now. Then it uses that value to determine the current millisecond number with which to initialize the variable Start.

When you click the "Check Elapsed Milliseconds" on your page, it calls your decider() function. decider() calls the function elapsedmilliseconds() to determine the number of milliseconds that have elapsed since the page was first loaded -- when the variable Start was initialized.

When elapsedmilliseconds() is called, it determines the difference between it's own calculation and the value stored in the variable Start. It stores the result in variable diff.

Then elapsedmilliseconds() returns the value of the variable diff, which is what decider() assigns to its own variable d.

Last, decider() decides whether or not the page has been displayed a total of less than 10 minutes (600,000 milliseconds). If true, it displays an alert box with the value stored in d. Otherwise, it sends the visitor off to

Hopefully, this has given you a glimmer of what is possible. Not only can you call your elapsedmilliseconds() function whenever you want, but other functions can call it, too, and make decisions based on what it returns.

Further elaboration and uses for program decision and flow control statements such as if(), for(), and while() will be in another section of this tutorial series.

(By the way, it took me 243740 milliseconds to write the above seven paragraphs while eating three small handfuls of popcorn.)

Let's suppose you want to modify your decider() function so you can tell it how many seconds to wait before sending the visitor off somewhere. And let's suppose you also want to tell it the location of that somewhere.

So you will modify your decider() function to accept a number (for the number of seconds) and a string of characters (for the URL). It will also be modified to use those data units where appropriate.

Here is the modified decider() function:

function decider(s,url)
var d = elapsedmilliseconds();
s = s * 1000; // s multiplies itself by 1000
if(d < s) { alert("Elapsed milliseconds: " + d); }
else { window.location = url; }

The decider() function receives two units of data when it
is called, the number of seconds and the url, and stores
them in variables s and url, respectively.

The variable "d" is then compared with "s" to see whether
the alert box is displayed or the visitor is redirected to
the value in "url".

You can call your modified decider() function with


or with

(90 seconds)

(45 seconds)

The above will send your function either the number 90 or the number 45 (for the number of seconds) and the string of characters (for the URL).

In a later article of this series, you will learn how to type stuff into a form (such as the number of seconds and the url, for the above example) and send that data to the function -- rather than hard-coding the data into a link.

And, you will often want to display function results in ways other than plain alert boxes.

Future articles will help you build popup boxes with your own design, on demand. They will help you display text and graphics on your page depending on which browser your visitor is using, depending on a name typed into a form, or other decisions your custom functions make.

Much of what you do with JavaScript depends on your program making decisions based on the contents of variables:

if([something]) then do this, else do that.

while([something]) do this.

for([each of a series]) do this.

The next article deals with those and other methods of program flow control.

Happy Happy!

William Bontrager, Programmer and Publisher "Screaming Hot CGI" programs "WillMaster Possibilities" ezine Copyright 2000 by William and Mari Bontrager

JavaScript Tutorial Part III


Click Here

Click here

Click Here

eTips Member Login

Enter your email address and password to enter the private membership area:

Lost Password?

Not yet a member?

Click here to see what you're missing! Club members receive access to dozens of free video tutorials, utilities and ebooks.

Products Products:

• Web Design Mastery
• eBook Starter

Other Products:

• Video Training Vault
• Plug-in Profits
• Affiliate Showcase
• Web Site Templates
• Business Tools
• Web Hosting

Our Other Sites

• Web Design Mastery
• eBook Starter
• Online Forex Trading
• Real Estate Investing
• Raging Hearts

Web Site Design

• HTML Tips
• PC Security
• CSS Tutorial
• HTML Codes
• Web Design Tips
• JavaScript Codes
• JavaScript Tutorial
• Java Applet Tutorial
• 216 Web Safe Colors
• Web Site Development
• ASCII Character Codes

Web Site Tools

• Domain Search
• Format Your Text
• Create Meta Tags
• Article Syndication
• Affiliate Showcase
• Free Classified Ads

Special SiteSell Promotion
Click Here

Daily News For

No comments:

liveCricket Headline Animator