ponydg
2018-06-03 01:06
<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> var num=0; function numCount(){ document.getElementById('txt').value=num; num=num+1; setTimeout("numCount()",1000); } </script> </head> <body> <form> <input type="text" id="txt" /> <input type="button" value="Start" /> </form> </body> </html>
点击事件不是只执行一次吗,底层是怎么运作的?
setTimeout()计时是只执行一次numCout()函数,但是
function numCount(){ document.getElementById('txt').value=num; num=num+1; setTimeout("numCount()",1000); }
这个函数内部又嵌套执行一次,这样就一直循坏嵌套执行下去啦。
<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> var num=0; function numCount(){ document.getElementById('txt').value=num; num=num+1; setTimeout("numCount()",1000); } </script> </head> <body> <form> <input type="text" id="txt" /> <input type="button" value="Start" onClick="numCount()" /> </form> </body> </html>
JavaScript进阶篇
468060 学习 · 21891 问题
相似问题