编程语言
首页 > 编程语言> > javascript – Javasacript倒数计时器,以天,小时,分钟,秒为单位

javascript – Javasacript倒数计时器,以天,小时,分钟,秒为单位

作者:互联网

我正在尝试创建一个基于时间的倒计时时钟.它不是基于current_dates.将被拉出的初始时间将来自一个单独的php文件.这将是基于浏览器的游戏.当有人单击按钮以启动此脚本时.它将检查是否满足某些要求,如果是,那么此脚本将启动.根据对象的级别,它将拉出该进程级别的初始计时器.希望有道理.无论如何我将计时器脚本从我提供的第一个代码中删除.

此脚本仅占用分钟和秒.我将其修改为包括日期和小时.在这个过程的某个地方,我搞砸了,脚本根本不起作用.我也不太确定这是否是计算这个的最佳方法.所以,如果你有一个更干净的方法,请分享.先感谢您.

这个脚本基于我看到的分钟/秒脚本.这是原始来源:

<span id="countdown" class="timer"></span>
<script>
   var seconds = 60;
   function secondPassed() {
   var minutes = Math.round((seconds - 30)/60);
   var remainingSeconds = seconds % 60;
   if (remainingSeconds < 10) {
      remainingSeconds = "0" + remainingSeconds; 
   }
   document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
   if (seconds == 0) {
    clearInterval(countdownTimer);
    document.getElementById('countdown').innerHTML = "Buzz Buzz";
   } else {
    seconds--;
   }
   }
   var countdownTimer = setInterval('secondPassed()', 1000);
</script>

这是我尝试包含天,小时,分钟和秒的修改过的脚本.

<span id="countdown"></span>
<script>
     var current_level = 93578;

     function timer() {

        var days = Math.round(current_level/86400);
        var remainingDays = Math.round(current_level - (days * 86400));

        if (days <= 0){
             days = current_level;
        }

        var hours = Math.round(remainingDays/3600);
        var remainingHours = Math.round(remainingDays - (hours * 3600));

        if (hours >= 24){
             hours = 23;
        }

        var minutes = Math.round(remainingHours/60);
        var remainingMinutes = Math.round(remainingHours - (minutes * 60));

        if (minutes >= 60) {
             minutes = 59;
        }

        var seconds = Math.round(remainingMinutes/60);

        document.getElementById('countdown').innerHTML = days + ":" + hours ":" + minutes + ":" + seconds;

        if (seconds == 0) {
             clearInterval(countdownTimer);
             document.getElementById('countdown').innerHTML = "Completed";
        }
     }
     var countdownTimer = setInterval('timer()', 1000);
</script>

解决方法:

我终于回过头来看这个并重新编写代码,这就像一个魅力.

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);
<span id="countdown" class="timer"></span>

标签:javascript,php,countdowntimer,timer
来源: https://codeday.me/bug/20190927/1822976.html