title: How to have server side countdown timer in javascript?
body: <p>I am currently building this timer on a webview where the timer will automatically updates with javascript and the timer will decrement every second.</p>
  
  <p>The current problem is that when a user makes the phone go to sleep, the timer doesn't update accordingly and the countdown will be a bit off so I would want to update the timer through server side instead of relying on a function that will break if a user makes the phone go to sleep. Here is the current code that I have </p>
  
  <pre><code>&lt;h2 id="timer"&gt; 
     1:00
  &lt;h2&gt;
  
  &lt;script&gt;
     var countdown = new Date("&lt;?php echo $row['end_timer'] ?&gt;").getTime();
     var now = new Date("&lt;?php echo date('Y-m-d H:i:s') ?&gt;")
  
       var x = setInterval(function() {
  
           // Increase the distance by 1
           now.setSeconds(now.getSeconds() +                                           
  
           var distance = countdown - (now.getTime());
  
           var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  
            document.getElementById("timer").innerHTML = (seconds &lt; 10 ? '0' : '') + seconds;
  
            if (distance &lt; 0) {
               clearInterval(x);
               document.getElementById("timer").innerHTML = "00";
               location.reload();
             }
       }, 1000);
  &lt;/script&gt;
  </code></pre>