为什么我的倒计时功能不起作用?
Why is my countdown function not working?
我对圣诞节感到非常兴奋,所以我决定有什么比创建一个圣诞节倒数计时器更好的倒数方式。但是我的倒计时似乎不起作用。
let targetDate = new Date(null, 11, 31, 0, 0, 0, 0)
let today = new Date()
targetDate.setFullYear(today.getFullYear())
if (targetDate.getTime() - today.getTime() <= 0) {
targetDate.setFullYear(today.getFullYear() + 1)
}
let timer = setInterval(() => {
today = new Date()
e = document.getElementById("countdown")
if (targetDate.getTime() - today.getTime() == 0) {
e.innerHTML = "Horray! Merry Christmas, Everyone! Horray!"
clearInterval(timer)
} else {
distance = targetDate.getTime() - today.getTime()
days = Math.floor(distance / (24 * 60 * 60 * 1000))
distance -= day * 24 * 60 * 60 * 1000
hours = Math.floor(distance / (60 * 60 * 1000))
distance -= hours * 60 * 60 * 1000
minutes = Math.floor(distance / (60 * 1000))
distance -= minutes * 60 * 1000
seconds = Math.floor(distance / 1000)
e.innerHTML = days + " d " hours + " h " + minutes + " m " + seconds + " s "
}
}, 1000)
我试过控制台记录定时器值,但代码没有呈现。 innerHTML 代码似乎没有 运行.
看看这一行
e.innerHTML = days + " d " hours + " h " + mi
应该是
e.innerHTML = days + " d " + hours + " h " + mi
我对圣诞节感到非常兴奋,所以我决定有什么比创建一个圣诞节倒数计时器更好的倒数方式。但是我的倒计时似乎不起作用。
let targetDate = new Date(null, 11, 31, 0, 0, 0, 0)
let today = new Date()
targetDate.setFullYear(today.getFullYear())
if (targetDate.getTime() - today.getTime() <= 0) {
targetDate.setFullYear(today.getFullYear() + 1)
}
let timer = setInterval(() => {
today = new Date()
e = document.getElementById("countdown")
if (targetDate.getTime() - today.getTime() == 0) {
e.innerHTML = "Horray! Merry Christmas, Everyone! Horray!"
clearInterval(timer)
} else {
distance = targetDate.getTime() - today.getTime()
days = Math.floor(distance / (24 * 60 * 60 * 1000))
distance -= day * 24 * 60 * 60 * 1000
hours = Math.floor(distance / (60 * 60 * 1000))
distance -= hours * 60 * 60 * 1000
minutes = Math.floor(distance / (60 * 1000))
distance -= minutes * 60 * 1000
seconds = Math.floor(distance / 1000)
e.innerHTML = days + " d " hours + " h " + minutes + " m " + seconds + " s "
}
}, 1000)
我试过控制台记录定时器值,但代码没有呈现。 innerHTML 代码似乎没有 运行.
看看这一行
e.innerHTML = days + " d " hours + " h " + mi
应该是
e.innerHTML = days + " d " + hours + " h " + mi