我正在尝试使计时器在第一个倒计时结束后自动更改为第二个new Date()
。这样做的目的是在需要更改倒计时日期的情况下,它不再需要手动编辑和上传js文件。但是我还没有弄清楚怎么做。下面是我尝试过的脚本,输出为“ EXPIRED”。我在哪里做错了?感谢您的支持!
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2020 14:00:00").getTime();
var countDownDate2 = new Date("Jan 15, 2020 14:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
var distance2 = countDownDate2 - now;
var a = distance;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(a / (1000 * 60 * 60 * 24));
var hours = Math.floor((a % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((a % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((a % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// Change to another exp.
if (distance < 0 && distance2 >0) {
function changeDate() {
a = distance2;
}
}
// If the count down is over, write some text
else {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
问题是您查看距离是否小于1且distance2大于1。但是,当距离仍处于活动状态时(在撰写本文时就是这种情况),它将自动进入else分支。