Javasacript Countdown timer in Days, Hours, Minute, Seconds

后端 未结 5 1221
慢半拍i
慢半拍i 2020-12-06 02:06

I\'m trying to create a time-based count down clock. It is not based upon current_dates. The initial time that will be pulled will be from a separate php file. This will be

5条回答
  •  囚心锁ツ
    2020-12-06 02:31

    I finally got back to looking at this and re-wrote the code and this works like a charm.

    var upgradeTime = 172801;
    var seconds = upgradeTime;
    function timer() {
      var days        = Math.floor(seconds/24/60/60);
      var hoursLeft   = Math.floor((seconds) - (days*86400));
      var hours       = Math.floor(hoursLeft/3600);
      var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
      var minutes     = Math.floor(minutesLeft/60);
      var remainingSeconds = seconds % 60;
      function pad(n) {
        return (n < 10 ? "0" + n : n);
      }
      document.getElementById('countdown').innerHTML = pad(days) + ":" + pad(hours) + ":" + pad(minutes) + ":" + pad(remainingSeconds);
      if (seconds == 0) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "Completed";
      } else {
        seconds--;
      }
    }
    var countdownTimer = setInterval('timer()', 1000);

提交回复
热议问题