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

来源:8-5 计时器setTimeout()

慕斯卡6904759

2018-07-05 21:20

<!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>


写回答 关注

4回答

  • 一叶红尘
    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>


JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468194 学习 · 21891 问题

查看课程

相似问题