admin管理员组文章数量:1302278
What is wrong with this code I seem to be geting an error that timer is not defined
var counter = setInterval("timer()",1000);
function timer(){
count = count-1;
if(count <=0){
clearInterval(counter);
return;
}
document.getElementById("timer").innerHTML = count + " sec";
}
What is wrong with this code I seem to be geting an error that timer is not defined
var counter = setInterval("timer()",1000);
function timer(){
count = count-1;
if(count <=0){
clearInterval(counter);
return;
}
document.getElementById("timer").innerHTML = count + " sec";
}
Share
Improve this question
asked Jul 11, 2012 at 17:30
Nistor AlexandruNistor Alexandru
5,3939 gold badges50 silver badges71 bronze badges
1
- You need to pass the function, not a string, as first param. – Gabriel Santos Commented Jul 11, 2012 at 17:34
1 Answer
Reset to default 9Don't pass a string to setInterval
.
Your function is a local variable, which doesn't exist when setTimeout
eval's the string in the global scope.
Instead, pass the function itself to setInterval
:
var counter = setInterval(timer, 1000);
本文标签: Javascript setInterval function not definedStack Overflow
版权声明:本文标题:Javascript setInterval function not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741668833a2391478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论