<!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>