为了账号安全,请及时绑定邮箱和手机立即绑定

定时器bug问题

定时器bug问题

杨__羊羊 2018-09-13 13:10:30
<!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);
}


查看完整回答
反对 回复 2018-10-09
  • 1 回答
  • 0 关注
  • 580 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信