JS之Window常见方法(三)计时器重复

计时器方法

setInterval()  按照指定的周期(以毫秒计)来调用函数或计算表达式

两个参数:

第一个参数:要执行的任务

第二个参数:多长时间(毫秒值)

clearInterval()  取消由 setInterval() 设置的 timeout

 1 <script>
 2     var id;
 3         window.onload = function(){
 4             var open = document.getElementById("open");
 5         open.onclick = function(){
 6             //计时(重复性)
 7             id = setInterval("aaa()",2000);
 8             }
 9         var close = document.getElementById("close");
10         close.onclick = function(){
11             //关闭计时
12              clearInterval(id);
13         }
14     }
15      function aaa(){
16         alert("来了");
17     }
18 </script>
19 <input type="button" value="开始计时" id="open" />
20 <input type="button" value="关闭计时" id="close" />

 

posted @ 2022-05-07 12:42  hi123hi159  阅读(94)  评论(0)    收藏  举报