首页 > 解决方案 > 在一个文档中单独观看时间

问题描述

我找到了一个倒计时的脚本。在每个脚本中,我设置了不同的结束时间。保存文件后,每个元素中的时间都是相同的。有谁知道如何分别<div>为每个脚本解决每个脚本中的时间?

<script>
// Set the date we're counting down to
var countDownDate = new Date("April 3, 2019 01:59:59").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 and 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("clockfirst").innerHTML = days + "d " + hours + "h "
    + minutes + "m " + seconds + "s ";

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

我将上面的代码复制并插入了三遍到一个文档中,并更改了每个脚本中的内容:

在脚本下面我添加了html标签:

<div id="clockfirst"></div>
<div id="clocksecond"></div>
<div id="clockthird"></div>

保存后每个元素显示相同的时间: 在此处输入图像描述

@mplingjan 我在这个主题之前检查过并没有解决我的问题,这就是我创建新主题的原因。是的,我为每个脚本更改了 setInterval,这对我来说没有解决问题。

标签: javascript

解决方案


推荐阅读