<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>计时器</title><script type="text/javascript"> var num=0; setTimeout(function startCount() { document.getElementById('count').value=num; num=num+1; setTimeout("startCount()",1000); },1000);</script></head><body><form><input type="text" id="count" /></form></body></html>
3 回答
小郎Lie
TA贡献2条经验 获得超0个赞
我也很好奇为什么必须把setTimeout("startCount()", 1000); 改成setTimeout(startCount, 1000);
虽然调试的结果确实是当使用setTimeout("startCount()", 1000);的时候,浏览器居然会报错VM325:1 Uncaught ReferenceError: startCount is not defined at <anonymous>:1:1
更神奇的是,如果我用setTimeout(startCount(),1000); 调试运行居然会一直调用startCount(),导致浏览器报错setTimeOut.html:8
求大神解答
qq_迸发激情_0
TA贡献1条经验 获得超0个赞
为什么要这样改
setTimeout("startCount()", 1000);
改成
setTimeout(startCount, 1000);
上面的不是等效的吗?
hahhhha
TA贡献50条经验 获得超32个赞
下面的代码试一下效果。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>计时器</title> </head> <body> <form> <input type="text" id="count" /> </form> </body> <script type="text/javascript"> (function(){ var ele = document.getElementById('count'), num = 0; var startCount =function(){ ele.value = num++; setTimeout(startCount,1000); }; startCount(); }()); </script> </html>
添加回答
举报
0/150
提交
取消