javascript - 倒数计时器重置日期

标签 javascript html

我尝试获取以下代码来在达到指定日期后重置计时器,以便它在下周重新启动。请帮忙!!

<script>
    // Set the date we're counting down to
    var countDownDate = new Date("Aug 26, 2017 0:0:0").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);

    // Display the result in the element with id="demo"
    document.getElementById("demo").innerHTML = days + "d " + hours + "h "
    + minutes + "m " + seconds + "s ";

    // If the count down is finished, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "GAME DAY";
    }
}, 1000);
</script>

最佳答案

将 if 部分替换为:

if (distance < 0) {
    document.getElementById("demo").innerHTML = "GAME DAY";
    if(distance < - 1000 * 60 * 60* 24){ // if its past the "game day"
        // reset timer to next week
        countDownDate += 1000 * 60 * 60 * 24 * 7
    }
}

关于javascript - 倒数计时器重置日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45890921/

相关文章:

javascript - 模块异步函数中的变量范围

javascript - Ajax不会调用Codeigniter Controller 的方法

javascript - 使用 knockout 将数组中某些输入的值与跨度绑定(bind)

php - anchor 在列内对齐

python - Python 中的类似对象的网站设计

javascript - 返回匹配的先行组

javascript - 在javascript中播放音频时更改左/右平衡

html - 选择下一个兄弟,仅当显示此跨度时

javascript - 文档上的 document.click()

html - 当换行时,自动在 `code` 环境中添加换行符号