title: Countdown to midnight, refresh every day
body: <p>I have this countdown script, it's a simple countdown.</p>
  
  <p>I need to update this countdown to run untl the mignight of the day, so when the user access in different hours will calculate the countdown until midnight and it refreshs every day.</p>
  
  <p>Ho can I do this?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var timeInMinutes = 1440;
  var currentTime = Date.parse(new Date());
  var deadline = new Date(currentTime + timeInMinutes * 60 * 1000);
  
  function getTimeRemaining(endtime) {
    var t = Date.parse(endtime) - Date.parse(new Date());
    var seconds = Math.floor((t / 1000) % 60);
    var minutes = Math.floor((t / 1000 / 60) % 60);
    return {
      'total': t,
      'minutes': minutes,
      'seconds': seconds
    };
  }
  
  function initializeClock(id, endtime) {
    var clock = document.getElementById(id);
    var minutesSpan = clock.querySelector('.minutes');
    var secondsSpan = clock.querySelector('.seconds');
  
    function updateClock() {
      var t = getTimeRemaining(endtime);
  
      minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
      secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
  
      if (t.total &lt;= 0) {
        clearInterval(timeinterval);
      }
    }
  
    updateClock();
    var timeinterval = setInterval(updateClock, 1000);
  }
  
  initializeClock('clockdiv', deadline);
  initializeClock('clockdiv2', deadline);</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="clockdiv"&gt;&lt;span class="minutes"&gt;&lt;/span&gt;:&lt;span class="seconds"&gt;&lt;/span&gt;&lt;/div&gt;
  
  &lt;div id="clockdiv2"&gt;&lt;span class="minutes"&gt;&lt;/span&gt;:&lt;span class="seconds"&gt;&lt;/span&gt;&lt;/div&gt;
  
  &lt;div id="clockdiv3"&gt;&lt;span class="minutes"&gt;&lt;/span&gt;:&lt;span class="seconds"&gt;&lt;/span&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>