title: JavaScript Countdown Timer Using Server Time
body: <p>I'm trying to create a countdown timer that's based on a time on the server.</p>
  
  <p>I originally had the server set the time left, and just did a <code>setTimeout</code> for 1 second that would decrement the time.</p>
  
  <p>I found 2 problems with this:</p>
  
  <ol>
  <li>There is a lag from the server setting the time until the client's page is rendered and the JavaScript begins to run. The lag amount depends on the speed of the users internet connection and computer/JavaScript engine.</li>
  <li>I think <code>setTimeout</code> of 1 second may have been getting behind a little on slower computers.</li>
  </ol>
  
  <p>I changed it so the server would set the ending time and the JavaScript on the client would take the time (in UTC) and calculate the remaining time left. It would then do this on every <code>setTimeout</code> callback. This makes the time and countdown perfect. If the client has a fast computer/JavaScript engine, the timer stays on page. If the computer/JavaScript engine is slower, you may see a second be skipped here and there, but the time is never off.</p>
  
  <p>I found 1 problem with this method so far:</p>
  
  <ol>
  <li>Every client's clock may be different.</li>
  </ol>
  
  <p>So, the time left may be a couple seconds, or 30 seconds, or even days off if the clients time is not correct on their computer.</p>
  
  <p>Is there a way that I can have the time left be exact based on the server's ending date?</p>