问答详情
源自:8-5 计时器setTimeout()

在点击按钮的时候不能停止,求条件语句代码

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>计时器</title>

</head>

<script type="text/javascript">

  var num=0;

  function startCount() {

    document.getElementById('count').value=num;

    num=num+1;

    setTimeout("startCount()",1000); 

  }

</script>

</head>

<body>

<form>

<input type="text" id="count" />

<input type="button" value="start" onclick="startCount()"/>

</form>

</body>

</html>


提问者:慕斯卡6904759 2018-07-05 21:20

个回答

  • 一叶红尘
    2018-07-29 13:42:00

    这个很有意思  当多点击几次按钮,计数会越来越快!!!

  • qq_抛物线_0
    2018-07-09 23:40:48

    clearInterval(timer); 先清除一下定时器,document.getElementById('status').value = 2 设置值

  • 慕斯卡6904759
    2018-07-08 22:09:35

    clearInterval(timer);

    document.getElementById('status').value = 2;

    这个是什么意思呢?谢谢

  • qq_抛物线_0
    2018-07-06 09:06:15

    <!DOCTYPE HTML>

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <title>计时器</title>

    </head>

    <script type="text/javascript">

    var num = 0;

    var timer = null;

    function startCount() {

    var statusValue = document.getElementById('status').value;

    if (statusValue == 1) {

    clearInterval(timer);

    document.getElementById('status').value = 2;

    timer = setInterval(function () {

    num = num + 1;

    document.getElementById('count').value = num;

    }, 1000);

    document.getElementById('btn').value = "暂停";

    } else {

    clearInterval(timer);

    document.getElementById('btn').value = "开始";

    document.getElementById('status').value = 1;

    }

    }

    </script>

    </head>

    <body>

    <form>

    <input type="hidden" id="status" value="1">

    <input type="text" id="count">

    <input id="btn" type="button" value="开始" onclick="startCount()">

    </form>

    </body>

    </html>