js进阶篇中8-6取消计时器clearTimeout()代码如下
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>计时器</title> </head> <script type="text/javascript"> var num=0; var i; function startCount(){ document.getElementById('count').value=num; num=num+1; i=setTimeout("startCount()",1000); } function stopCount(){ clearTimeout(i); } </script> </head> <body> <form> <input type="text" id="count" /> <input type="button" value="Start" onclick="startCount()" /> <input type="button" value="Stop" onclick="stopCount()" /> </form> </body> </html>
点击多少次Start按钮,就要点击多少次Stop按钮才能停在计时器。我想问此时i值是怎么保存setTimeout()的id值,通过数组吗?假设是数组的话,clearTimeout(i)方法是用shift()删除数组i里的元素和splice()添加元素的?
pardon110
相关分类