为HTML5标语项目构建倒数计时器。
无法弄清楚如何使天/小时/分钟/秒为两位数。
07而不是7。
<script>
const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24,
countdownDate = "Nov 27, 2020 00:00:00"; // 24hr time
let countDown = new Date(countdownDate).getTime(),
x = setInterval(function() {
//calculate countdown based on current time
let now = new Date().getTime(),
distance = countDown - now;
document.getElementById("days").innerText = Math.floor(distance / (day)),
document.getElementById("hours").innerText = Math.floor((distance % (day)) / (hour)),
document.getElementById("minutes").innerText = Math.floor((distance % (hour)) / (minute)),
document.getElementById("seconds").innerText = Math.floor((distance % (minute)) / second);
//do something later when date is reached
if (distance < 0) {
let headline = document.getElementById("headline"),
countdown = document.getElementById("countdown"),
content = document.getElementById("content");
headline.innerText = "Now Open";
countdown.style.display = "none";
content.style.display = "block";
clearInterval(x);
}
}, second)
</script>