JavaScript 倒计时器
我需要一个可以显示秒:毫秒格式的倒计时器,我发现一个我认为可以修改它以显示为 4:92 的计时器,但由于某种原因它不想为我工作。它在网站上运行良好,但我尝试将其放入我的页面中,控制台告诉我:
Uncaught ReferenceError:显示未定义
。
我做错了什么?
var milisec=0
var seconds=30
document.getElementById("timer").innerHTML='30'
function display(){
if (milisec<=0){
milisec=9
seconds-=1
}
if (seconds<=-1){
milisec=0
seconds+=1
}
else
milisec-=1
document.getElementById("timer").innerHTML=seconds+"."+milisec
setTimeout("display()",100)
}
display()
(原始来源)
I need a count down timer that can display second:miliseconds format, I found one that I figured I could modify it to show this like 4:92 but it doesn't want to work for me for some reason. It works fine on the site, but I try and put it into my page, the console tells me:
Uncaught ReferenceError: display is not defined
.
What did I do wrong?
var milisec=0
var seconds=30
document.getElementById("timer").innerHTML='30'
function display(){
if (milisec<=0){
milisec=9
seconds-=1
}
if (seconds<=-1){
milisec=0
seconds+=1
}
else
milisec-=1
document.getElementById("timer").innerHTML=seconds+"."+milisec
setTimeout("display()",100)
}
display()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使其
setTimeout( display, 100 )
以便传递文字,否则它将在全局上下文中执行,并且很可能 fn 没有定义为 window 的方法(可能是因为您有一个窗口加载字面意思?)Make it
setTimeout( display, 100 )
so the literal is passed, otherwise it executes in global context and most likely that fn is not defined as a method of window ( maybe because you have it a window load anon literal? )