Javasacript 倒数计时器,单位为天、小时、分钟、秒

2022-08-31 00:07:50

我正在尝试创建一个基于时间的倒计时时钟。它不是基于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>

答案 1

我终于回到这个并重写了代码,这就像一个魅力。

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>

答案 2

就个人而言,我会使用jquery倒数计时器集成。它很简单,并且具有许多选项可以以不同的格式显示。由于我对JS也相当陌生,因此这是我找到的获取计数/计时器的最简单方法。

http://keith-wood.name/countdown.html