Javascript 定时器

在js中定时器分两种:setTimeout()和setInterval()

 

1.setTimeOut():只在指定时间后执行一次

/定时器 异步运行  
function hello(){  
alert("hello");  
}  
//使用方法名字执行方法  
var t1 = window.setTimeout(hello,1000);  
var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法  
window.clearTimeout(t1);//去掉定时器

 

2.setInterval():以指定时间为周期循环执行

/实时刷新  时间单位为毫秒  
setInterval('refreshQuery()',8000);   
/* 刷新查询 */  
function refreshQuery(){  
  console.log('每8秒调一次') 
}

 

posted @ 2018-10-14 15:50  小鸽鸽OvO  阅读(172)  评论(0编辑  收藏  举报