[英]Multiple countdowns on the same page JS
I know nothing about JS so please cut me some slack. 我对JS一无所知,所以请给我一些时间。 I am using a countdown from the W3schools website.
我正在使用W3schools网站的倒计时。 The problem is that even though I give the two countdowns separate IDs, it seems that they are running the same countdown, rather than being separate.
问题是,即使我给两个倒计时分别提供了ID,但它们似乎在运行相同的倒计时,而不是分开。 Both seem to run sec-cd.
两者似乎都运行sec-cd。
How can allow these two countdowns to work separately? 如何允许这两个倒数计时分别工作?
HTML 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 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 Link: https://jsfiddle.net/az0upkxu/ JSFiddle链接: https ://jsfiddle.net/az0upkxu/
This is because you are overriding same variable. 这是因为您要覆盖相同的变量。 Kindly use another variable.
请使用另一个变量。
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
also use these different variable names inside the code using these parameters. 还可以使用这些参数在代码内使用这些不同的变量名称。
Below code will work. 下面的代码将起作用。
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);
You're using the countDownDate
variable twice. 您正在使用
countDownDate
变量两次。 Just re-declare it has countDownDateTwo
and also change distance
to equal countDownDateTwo - now
and it works. 只需重新声明它具有
countDownDateTwo
并将distance
更改为等于countDownDateTwo - now
就可以了。
Be careful with variable names, this is why scope is key. 注意变量名,这就是范围是关键的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.