為什么連續點擊start速度會變快,而且stop不下來
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計時器</title>
<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>
2019-08-15
在startCount函數里第一行加?clearTimeout(i);可以解決這個問題
2019-01-10
同學代碼里的第一個代碼就解決這個問題
2019-01-06
很有趣的發現
2018-12-31
可以停止, 你點了多少次start. 就點多少次 stop.
2018-12-17
因為你不停的在打開定時器,你開了無數個。。所以導致程序亂了。。