我正在尝试用Javascript做一个倒计时。我现在有代码,它工作得很好,但我想有更多的日期。所以第一个倒计时是2022年5月12日16:04:00,然后我想从2022年5月14日16:04:00开始一个新的48小时倒计时,2022年5月16日16:04:00,以此类推。所以当它是2022年5月12日16:04:01时,我想要一个新的48小时计时器开始并倒计时到5月14日。这可能吗?谢谢你的帮助!
var countDownDate = new Date("May 12, 2022 16:04:00").getTime();
var x = setInterval(function () {
var now = new Date().getTime();
var distance = countDownDate - now;
var hours = Math.floor((distance % (1000 * 120 * 120 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("hours").innerHTML = hours;
document.getElementById("minutes").innerHTML = minutes;
document.getElementById("seconds").innerHTML = seconds;
if (distance < 0) { clearInterval(x); document.getElementById("countdown-text").innerHTML = "48H"; }
}, 1000);
<div id="countdown">
<div class="countdown-hours">
<p id="hours"></p><span>hours</span>
</div>
<div class="countdown-minutes">
<p id="minutes"></p><span>minutes</span>
</div>
<div class="countdown-seconds">
<p id="seconds"></p><span>seconds</span>
</div>
</div>
var countDownDate = new Date("May 12, 2022 16:04:00").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var hours = Math.floor((distance % (1000 * 120 * 120 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("hours").innerHTML = hours;
document.getElementById("minutes").innerHTML = minutes;
document.getElementById("seconds").innerHTML = seconds;
if (distance < 0) {
var numberOfMlSeconds = new Date().getTime();
var addMlSeconds = 48 * 60 * 60 * 1000;
countDownDate = new Date(numberOfMlSeconds + addMlSeconds);
}
}, 1000);
<div id="countdown">
<div class="countdown-hours">
<p id="hours"></p><span>hours</span>
</div>
<div class="countdown-minutes">
<p id="minutes"></p><span>minutes</span>
</div>
<div class="countdown-seconds">
<p id="seconds"></p><span>seconds</span>
</div>
</div>