Friday, October 07, 2005

How does the Countdown Clock work?

Really simple. It updates 5 times per second. Each time it does the following:

1) Get your computer's current unix timestamp (number of seconds since 1st Jan, 1970 - but this date is relatively irrelevant)
2) Get the 26th Dec, 2005 3:00pm as a unix timestamp. (SC Start Time) The functions pay attention to Daylight Savings, and presume the current time zone. I could probably make it smart and know that it is AEST, but why?
3) Subtract the current time from the SC Start time and call it diff
4) Calculate the days, hours minutes and seconds using some fancy division and the int function
5) Chuck it all in a string and display it.

So you have something like:


var d1:Date = new Date();
var d2:Date = new Date(2005, 11, 26, 15, 0, 0, 0);
var nowTime:Number = d1.getTime();
var scTime:Number = d2.getTime();
var diff = int((scTime-nowTime)/1000);
var days = int(diff/(60*60*24));
diff = diff - (days*60*60*24);
var hours = int(diff/(60*60));
diff = diff - hours*60*60;
var minutes = int(diff/60);
diff = diff - minutes*60;
var seconds = int(diff);
countdownClock.text = days + " days " + hours + " hours\n" +
minutes + " minutes " + seconds + " seconds";

0 Comments:

Post a Comment

<< Home

Google