js setTimeout 与 setInterval 以及 for 循环 刷新UI
1. setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式;执行一次; 如果需要执行多次,自身再次调用 setTimeout(); 示例:无穷循环并带停止按钮的 <html> <head> <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } function stopCount() { clearTimeout(t) } </script> </head> <body> <form> <input type="button" value="开始计时!" onClick="timedCount()"> <input type="text" id="txt"> <input type="button" value="停止计时!" onClick="stopCount()"> </form> <p> 请点击上面的“开始计时”按钮。输入框会从 0 开始一直进行计时。点击“停止计时”可停止计时。 </p> </body> </html> 2. setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式;