Ich weiß nichts über JS, also bitte schneide mir etwas Durchhang. Ich verwende einen Countdown von der W3schools-Website. Das Problem ist, dass, obwohl ich die beiden Countdowns separate IDs gebe, scheint es, dass sie den gleichen Countdown laufen, anstatt getrennt zu sein. Beide scheinen sek-cd zu laufen.Mehrere Countdowns auf der gleichen Seite JS
Wie können diese beiden Countdowns getrennt arbeiten?
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-Link:https://jsfiddle.net/az0upkxu/
Bitte markieren Sie es als eine Antwort, wenn es hilft. –