March 18th, 07': Script completely
rewritten for multiple instances on the same page, plus ability to
customize output via "
oncountup" event handler.
Description: Display how much time has passed
since an expired date with this rare script. It's sensitive to the second (ie "
9, 2005 13:30:00"), and thanks to a custom "
handler, you can control exactly the action to take each second as the script is
counting up, most commonly, display the results inside a DIV.
Lets use it below to show how long "Prince Charles" has been
married to Camilla Parker for:
Step 1: Insert the below script into the HEAD section of
Step 2: Then, add the below sample count up code to
the BODY of your page to see how to initialize the script:
The code of Step 2 shows you how to create a count up.
First, call the
var princewedding=new dcountup("April 9, 2005 13:30:00", "days")
It accepts the following two parameters:
past_date_and_time_string: A string containing the
complete count up date and time using the format: "
April 9, 2005
13:30:00". As you can see, the time is in military (24 hour) format.
baseunit (string): The top level unit to count up
using. Valid values are "
seconds." If you enter "
hours", for example, the
script will count the number of hours, minutes, and seconds left until the
event, instead of starting at days.
Once you've called
dcountup(), the script begins
counting up, though no results are shown. It's up to you to tell the script
exactly what to do as each second passes, using the "
event handler of the script instance:
Whatever code you define inside this event handler will be run every
second as the script ticks away!
//Define action to take as script counts up each second
//Available properties: result["days"], result["hours"],
result["minutes"], and result["seconds"]
The lone parameter "
result" gives you access to the days, hours,
minutes, and seconds that together add up to the amount of time that has passed.
Each component is accessed via:
inside your custom function, respectively. So with that said, here's how you
might display the results of a countup in an arbitrary DIV on the page:
<div id="cpcontainer"> </div>
mycountainer.innerHTML="Prince Charles and Camilla Parker have been
married for:"+ result['days']+" days "+result['hours']+" hours
"+result['minutes']+" minutes "+result['seconds']+" seconds"
It's up to you to customize the action to take "
you take a look at the code of Step 2, you'll see I've formatted the results
using tags such as