View Full Version : Countdown Inaccuracy

08-17-2008, 06:12 PM
I recently decided to update one of my JavaScript Countdown scripts to be more accurate. Originally, JS obtained the current and future times and found the time remaining in milliseconds. Instead, I wanted to use the server time to prevent time zone differences and lack of synchronization of the user's clock from affecting the data. I've already gotten the time correctly injected into the script with PHP and the countdown functions, but it becomes inaccurate after a few seconds. Here's the primary code in use:

$Time = date("U", time());
$Countdown = (1219235100 - $Time) * 1000;

var countdown = <?php echo $Countdown; ?>;

countdown = countdown - 17.92;

countnanosec = Math.floor(countdown*1000000);
countmicrosec = Math.floor(countdown*1000);
countmillisec = Math.floor(countdown*1);
countsec = Math.floor(countdown/1000);


setTimeout("Countdown()", 1);

Essentially, it counts down either too fast or too slow. The only solution that works accurately is to decrease 1,000ms every 1,000ms (countdown = countdown - 1000; and setTimeout("Countdown()", 1000);). I'd like it to update a bit faster, however, I can't find two matching numbers that maintain accuracy at lower values. Updating every 1ms, 17.92 is close, about 3 Seconds off each additional minute.

What am I missing? Suggestions would be greatly appreciated.