1.setTimeOut()
只在指定时间后执行一次
//定时器 异步运行 function hello(){ alert("hello"); } //使用方法名字执行方法 var t1 = window.setTimeout(hello,1000); var t2 = window.setTimeout("helo()",3000);//字符串执行方法 window.clearTimeout(t1);//去掉定时器
2.setInterval()
以指定时间为周期循环执行
//实时刷新 时间单位为毫秒 setInterval('refreshQuery()',8000); //刷新查询 function refreshQuery(){ console.log("每8秒调一次") }
两种方法根据不同的场景和业务需求择而取之,
对于这两个方法,需要注意的是如果要求在每隔一个固定的时间间隔后就精确地执行某动作,那么最好使用setInterval,而如果不想由于连续调用产生互相干扰的问题,尤其是每次函数的调用需要繁重的计算以及很长的处理时间,那么最好使用setTimeout