问答详情
源自:8-4 取消计时器clearInterval()

我想做一个可以通过按钮随意开始和终止时间的代码,但是结束不了,怎么回事呢?

<!DOCTYPE HTML>

<html>

    <head>

        <script>

            function kaishi(){

                var abb= new Date();

                document.getElementById("con").value=abb;

                var i=setInterval(kaishi,1000);

            }

           

            </script>

        </head>

        <body>

            <input type ="text" id="con" size="60"/>

            <input type="button" value="jieshu" onclick="clearInterval(i)" />

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

            </body>

    </html>


提问者:慕前端0798377 2018-08-13 10:18

个回答

  • 吾知后世
    2018-08-13 15:04:57

    <html>

    <body>


    <input type="text" id="clock" size="35" />

    <script language=javascript>

    var int=self.setInterval("clock()",50)

    function clock()

      {

      var t=new Date()

      document.getElementById("clock").value=t

      }

    </script>

    <button onclick="int=window.clearInterval(int)">Stop interval</button>

    <button onclick="int=window.setInterval('clock()',50)">Start interval</button>


    </body>

    </html>


  • 吾知后世
    2018-08-13 11:55:02

    http://www.w3school.com.cn/jsref/met_win_setinterval.asp

  • qq_拥抱新的每一天_0
    2018-08-13 11:32:35

    你试试  这样就可以了

  • qq_拥抱新的每一天_0
    2018-08-13 11:32:04

    <!DOCTYPE HTML>

    <html>

        <head>

            <script>

                function kaishi(){

                   var abb= new Date();

                    document.getElementById("con").value=abb;

                }

                   var i=setInterval('kaishi()',1000);

               function end(){

                 clearInterval(i);

               }

               function start(){

                     i=setInterval('kaishi()',1000);

               }

                </script>

            </head>

            <body onload = 'kaishi();'>

                <input type ="text" id="con" size="60"/>

                <input type="button" value="jieshu" onclick="end()" />

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

            </body>

        </html>