<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> var time; var i=0; function c() { time=setInterval("d()",1000) } function d() { (document.getElementById('cd')).value=i++; } function f() { clearInterval(time); } </script></head><body> <input id='cd' type ="button" value ="number"> <input type ="button" value ="开始" onclick="c()"> <input type ="button" value ="停止" onclick="f()"></body></html>点了几次开始,数字就变得快的起来。我知道这是个bug,想知道为什么会越来越快?希望各位大神说的详细点,新手一枚,谢谢。
1 回答

ITMISS
TA贡献1871条经验 获得超8个赞
在 c()
里,如果你没清除上一次的 interval,会定义成多个,而且由于 time
被覆盖,以前定义的无法清除。稍改改就行
function c() { clearInterval(time); time = setInterval("d()", 1000); }
添加回答
举报
0/150
提交
取消