🎪JavaScript定时器方法

一、setTimeout() 延迟性操作

 1 window.setTimeout(function(){
 2     console.log('派大星');//延迟了4秒
 3 },4000);
 4 console.log('海绵宝宝');
 5 
 6 //定时器 异步运行  
 7 function hello(){  
 8     alert("hello");  
 9 }  
10   
11 var t1 = window.setTimeout(hello,1000);//使用方法名字执行方法  
12 var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法  

二、setInterval() 定时输出

 1 var num = 0;
 2 var timer = null;
 3 timer = setInterval(function(){
 4     num++;
 5     if(num>5){
 6         clearInterval(timer);
 7         return;
 8     }
 9     console.log('num:'+num);
10 },1000);//1秒输出一次
11 
12 
13 //实时刷新  时间单位为毫秒  
14 setInterval('refreshQuery()',8000);   
15 /* 刷新查询 */  
16 function refreshQuery(){  
17   console.log('每8秒调一次') 
18 }
原文地址:https://www.cnblogs.com/songhaixing/p/12011376.html