【问题标题】:Calculating countdown by hours \ minutes instead of seconds按小时\分钟而不是秒计算倒计时
【发布时间】:2018-04-23 07:53:33
【问题描述】:

我正在尝试使用此倒计时,但它每秒计算一次。 我想按分钟甚至小时计算时间,而不是每秒,因为我只显示日期和小时。

我已尝试删除 var 秒或分钟,但检查元素每 1 秒向我显示计时器上的紫色闪烁,这意味着它仍在按秒计算时间。

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 60px;
  margin-top:0px;
}
</style>
</head>
<body>

<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("April 24, 2018 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
    
    // Find the distance between now an the count down date
    var distance = countDownDate - now;
    
    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
    // Output the result in an element with id="demo"
    document.getElementById("demo").innerHTML = days + "d " + hours + "h ";
    
    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
</script>

</body>
</html>

【问题讨论】:

  • 什么现在不工作?
  • 计数器工作正常我只需要它每 1 分钟而不是 1 秒计算一次

标签: javascript time countdown


【解决方案1】:

改变

// Update the count down every 1 second
var x = setInterval(function() {
...
}, 1000);

// Update the count down every 1 minute
var x = setInterval(function() {
...
}, 1000*60);

// Update the count down every 1 hour
var x = setInterval(function() {
...
}, 1000*60*60);

编辑

这将每分钟/每小时更新一次您的计时器。现在,开始倒计时并每分钟/每小时更新一次:

calculateCountdown();
// Update the count down every 1 minute
var x = setInterval(function() {
  calculateDountdown();
}, 1000*60);

或者只是

calculateCountdown();
setInterval(calculateCountdown, 1000*60);

在哪里

function calculateCountdown() {
  // Get todays date and time
    var now = new Date().getTime();

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("demo").innerHTML = days + "d " + hours + "h ";

    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}

查看此answer,了解如何将基于setInterval 的解决方案重构为基于setTimeout

【讨论】:

  • 我尝试使用分钟更新,但它什么也没显示,但 1 分钟后它显示日期和小时
猜你喜欢
  • 2017-10-15
  • 1970-01-01
  • 2014-10-26
  • 2021-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
相关资源
最近更新 更多