为什么 setInterval 在尝试倒计时时不起作用
Why is setInterval not working when trying to make a countdown
我正在尝试在 javascript 中创建一个计数器,并且我使用 setInterval 每毫秒更改一次计时器,但它不会更改计时器。除非我重新加载页面,否则计时器是相同的。
我的代码:
HTML 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 id="final"> <b id="year"></b>:<b id="month"></b>:<b id="days"></b>:<b id="minutes"></b>:<b id="seconds"></b>:<b id="milli"></b></h1>
<h1 id="end" hidden>Counter is done</h1>
<script src="script.js"></script>
</body>
</html>
我的js:
year = document.getElementById("year")
month = document.getElementById("month")
days = document.getElementById("days")
milli = document.getElementById("milli")
minutes = document.getElementById("minutes")
seconds = document.getElementById("seconds")
currentDate = new Date()
snapDay = new Date("Nov 25, 2019 0:0:0")
it = setInterval(function(){
if(currentDate.getMinutes() == snapDay.getMinutes() && currentDate.getDate() == snapDay.getDate() && snapDay.getFullYear() == currentDate.getFullYear() && currentDate.getSeconds() == snapDay.getSeconds() && currentDate.getMilliseconds() == snapDay.getMilliseconds()){
final.hidden = true
end.hidden = false
clearInterval(it)
}
theMonth = currentDate.getMonth()
if(theMonth == 10){
month.innerHTML = "3"
}else if(theMonth == 11){
month.innerHTML = "2"
}else if(theMonth == 12){
month.innerHTML = "1"
}
year.innerHTML = snapDay.getFullYear() - currentDate.getFullYear()
days.innerHTML = currentDate.getDate() - snapDay.getDate()
minutes.innerHTML = currentDate.getMinutes() - snapDay.getMinutes()
seconds.innerHTML = currentDate.getSeconds() - snapDay.getSeconds()
milli.innerHTML = currentDate.getMilliseconds() - snapDay.getMilliseconds()
},01)
所以在@Pointy 帮助我之后,我发现错误是我在 setInterval 之外声明了 date() 而这不是 setInterval 问题。
我正在尝试在 javascript 中创建一个计数器,并且我使用 setInterval 每毫秒更改一次计时器,但它不会更改计时器。除非我重新加载页面,否则计时器是相同的。 我的代码: HTML 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 id="final"> <b id="year"></b>:<b id="month"></b>:<b id="days"></b>:<b id="minutes"></b>:<b id="seconds"></b>:<b id="milli"></b></h1>
<h1 id="end" hidden>Counter is done</h1>
<script src="script.js"></script>
</body>
</html>
我的js:
year = document.getElementById("year")
month = document.getElementById("month")
days = document.getElementById("days")
milli = document.getElementById("milli")
minutes = document.getElementById("minutes")
seconds = document.getElementById("seconds")
currentDate = new Date()
snapDay = new Date("Nov 25, 2019 0:0:0")
it = setInterval(function(){
if(currentDate.getMinutes() == snapDay.getMinutes() && currentDate.getDate() == snapDay.getDate() && snapDay.getFullYear() == currentDate.getFullYear() && currentDate.getSeconds() == snapDay.getSeconds() && currentDate.getMilliseconds() == snapDay.getMilliseconds()){
final.hidden = true
end.hidden = false
clearInterval(it)
}
theMonth = currentDate.getMonth()
if(theMonth == 10){
month.innerHTML = "3"
}else if(theMonth == 11){
month.innerHTML = "2"
}else if(theMonth == 12){
month.innerHTML = "1"
}
year.innerHTML = snapDay.getFullYear() - currentDate.getFullYear()
days.innerHTML = currentDate.getDate() - snapDay.getDate()
minutes.innerHTML = currentDate.getMinutes() - snapDay.getMinutes()
seconds.innerHTML = currentDate.getSeconds() - snapDay.getSeconds()
milli.innerHTML = currentDate.getMilliseconds() - snapDay.getMilliseconds()
},01)
所以在@Pointy 帮助我之后,我发现错误是我在 setInterval 之外声明了 date() 而这不是 setInterval 问题。