2017-08-12 50 views
2

我对JS一无所知,所以请减少一些松懈。 我正在使用W3schools网站的倒计时。 问题是,即使我给两个倒计时单独的ID,似乎他们正在运行相同的倒计时,而不是分开。两者似乎都运行sec-cd。在同一页面上进行多次倒计时JS

如何让这两个倒计时单独工作?

HTML

<div id="main"> 
    <h1><u><b>Countdown 1</b></u></h1> 
    <h1 id="main-cd"></h1> 
    <div class="secondary"> 
    <h1><u><b>Countdown 2</b></u></h1> 
    <h1 id="sec-cd"></h1> 
    </div> 

</div> 

的JavaScript

var countDownDate = new Date("August 29, 2017 19:00:00 GMT").getTime(); 
var x = setInterval(function() { 

    var now = new Date().getTime(); 

    var distance = countDownDate - now; 

    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); 

    document.getElementById("main-cd").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; 

    if (distance < 0) { 
    clearInterval(x); 
    var ongoing = document.getElementById("main-cd").innerHTML = "Fin"; 
    } 

}, 1000); 

var countDownDate = new Date("August 28, 2017 19:00:00 GMT").getTime(); 

var x = setInterval(function() { 

    var now = new Date().getTime(); 

    var distance = countDownDate - now; 

    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); 

    document.getElementById("sec-cd").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; 

    if (distance < 0) { 
    clearInterval(x); 
    var ongoing = document.getElementById("sec-cd").innerHTML = "Fin"; 
    } 

}, 1000); 

的jsfiddle链接:https://jsfiddle.net/az0upkxu/

回答

2

您使用的是countDownDate变量的两倍。只需重新声明它有countDownDateTwo并且还将distance更改为等于countDownDateTwo - now并且它有效。

小心变量名称,这就是为什么范围是关键。

2

这是因为你将覆盖同一个变量。请使用其他变量。

var countDownDate = new Date("August 29, 2017 19:00:00 GMT").getTime(); 

var countDownDate_2 = new Date("August 28, 2017 19:00:00 GMT").getTime();// renmaed variable 

也在使用这些参数的代码中使用这些不同的变量名称。

下面的代码将工作。

var countDownDate = new Date("August 29, 2017 19:00:00 GMT").getTime(); 
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("main-cd").innerHTML = days + "d " + hours + "h " 
    + minutes + "m " + seconds + "s "; 

    // If the count down is over, write some text 
    if (distance < 0) { 
     clearInterval(x); 
     var ongoing =document.getElementById("main-cd").innerHTML = "Event Ongoing"; 
    } 

}, 1000); 
// Set the date we're counting down to 
var countDownDate_2 = new Date("August 28, 2017 19:00:00 GMT").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_2 - 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("sec-cd").innerHTML = days + "d " + hours + "h " 
    + minutes + "m " + seconds + "s "; 

    // If the count down is over, write some text 
    if (distance < 0) { 
     clearInterval(x); 
     var ongoing =document.getElementById("sec-cd").innerHTML = "Season has eneded!"; 
    } 

}, 1000); 
+0

请将其标记为有帮助的答案。 –