[英]Why can't I see the countdown timer
I tried to make a countdown timer using setInterval()
. 我尝试使用setInterval()
制作倒数计时器。 I used code made by other people to learn how, but it failed. 我使用其他人制作的代码来学习如何,但它失败了。 After searching to find the issue, I was not able to find it. 在搜索找到问题后,我无法找到它。
My code: 我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Count</title>
<script="text/javascript">
var Stime = 10; //Set time(minute)
var Ssecond = Stime * 60;
var countdownID=0;
function inidown(){
countdownID = setInterval("Count()",1000);
}
function Count(){
if(Ssecond>0){
var rM=Ssecond/60;
var rS=Ssecond%60;
document.getElementById("count").innerHTML=rM+":"+rS;
Ssecond--;
}
else{
clearInterval(countdownID);
document.getElementById("out").innerHTML="time out";
}
}
</script>
</head>
<body onload="inidown()">
<div id="count"></div>
<div id="out"></div>
</body>
</html>
I saw that code on http://blog.niklasottosson.com/?p=665 我在http://blog.niklasottosson.com/?p=665上看到了该代码
var Stime = 10; //?? ??(?)
var Ssecond = Stime * 60;
var countdownID = 0;
function inidown() {
countdownID = setInterval("Count()", 1000);
}
function Count() {
if (Ssecond > 0) {
var rM = Ssecond / 60;
var rS = Ssecond % 60;
document.getElementById("count").innerHTML = rM + ":" + rS;
Ssecond--;
} else {
clearInterval(countdownID);
DivCount.innerHTML = "Time Out";
document.getElementById("out").innerHTML = "time out";
}
}
No error occurs in that code. 该代码中没有错误。
There was a problem with your script tag. 您的脚本标记出现问题。
Use Math.floor for your minutes. 使用Math.floor作为你的会议记录。
Pad the seconds for single digits. 填充单个数字的秒数。
<html> <head> <title>Count</title> <script type="text/javascript"> var Stime = 10; //Set time(minute) var Ssecond = Stime * 60; var countdownID = 0; function inidown() { countdownID = setInterval("Count()", 1000); } function Count() { if (Ssecond > 0) { var rM = Math.floor(Ssecond / 60); var rS = ("0" + (Ssecond % 60)).slice(-2); document.getElementById("count").innerHTML = rM + ":" + rS; Ssecond--; } else { clearInterval(countdownID); document.getElementById("count").innerHTML = ""; document.getElementById("out").innerHTML = "Time Out"; } } </script> </head> <body onload="inidown()"> <div id="count"></div> <div id="out"></div> </body> </html>
<script="text/javascript">
should have a type
attribute <script type="text/javascript">
<script="text/javascript">
应该具有type
属性<script type="text/javascript">
setInterval("Count()",1000);
works, but can also be rewritten as setInterval(Count,1000);
有效,但也可以重写为setInterval(Count,1000);
With these changes it is working. 通过这些更改,它正在运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.