JavaScript Countdown Timer to specific time everyday

前端 未结 2 466
余生分开走
余生分开走 2021-01-27 17:14

I am currently developing a website with a countdown timer at the headline: http://iphone.myhandykey.com/

The current timer is just 12hrs + few mins.. What I would like

2条回答
  •  囚心锁ツ
    2021-01-27 17:42

    (function() {
      var start = new Date;
      start.setHours(23, 0, 0); // 11pm
    
      function pad(num) {
        return ("0" + parseInt(num)).substr(-2);
      }
    
      function tick() {
        var now = new Date;
        if (now > start) { // too late, go to tomorrow
          start.setDate(start.getDate() + 1);
        }
        var remain = ((start - now) / 1000);
        var hh = pad((remain / 60 / 60) % 60);
        var mm = pad((remain / 60) % 60);
        var ss = pad(remain % 60);
        document.getElementById('time').innerHTML =
          hh + ":" + mm + ":" + ss;
        setTimeout(tick, 1000);
      }
    
      document.addEventListener('DOMContentLoaded', tick);
    })();
    Only  left!

提交回复
热议问题