js---定时器

自古美人都是妖i 提交于 2020-02-01 12:20:31
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            //window 对象是一个最顶层对象
            //window.setInterval("alert('123');",2000);//每个两秒弹出一次消息框
            
            
            function test(){
                console.log("setINterval 调用了");
            }
            setInterval("test()",2000);
            //如上实现一个函数的循环调用
            
            
            //setTimeout("test()",2000);//2000ms执行一次之后就不在执行。不具备setInterval的循环功能。
            
            var timerID;
            function start()
            {
                timerID=setInterval("test()",2000);
            }
            
            function stop()
            {
                clearInterval(timerID);//使用语法停止了计时器的运行
            }
        </script>
    </head>
    <body>
        <input type="button" value="开启定时器" onclick="start()" />
        <input type="button" value="停止定时器" onclick="stop()" />
    </body>
</html>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!