Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Countdown Inaccuracy

    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:

    PHP Code:
    $Time date("U"time());
    $Countdown = (1219235100 $Time) * 1000
    Code:
    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.
    Last edited by coolcamo8642; 08-17-2008 at 11:17 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •