Js、Jquery定时执行(一次或者重复多次,取消重复)

1. 创建一个变量
var ref = "";
2. 定时刷新调用的方法
function consoleLog(){
    console.log("a");
}
3. 设置定时刷新
ref = setInterval(function(){
    consoleLog();
},2000);
这样会每2秒执行一次consoleLog方法
  
4. 阻止定时刷新
clearInterval(ref);



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


<script> 
function hello(){ 
alert("hello"); 
} 
//重复执行某个方法 setInterval重复执行
var t1 = window.setInterval(hello,1000); 
var t2 = window.setInterval("hello()",3000); 
//去掉定时器的方法 
window.clearInterval(t1); 
</script> 


posted @ 2016-12-19 11:06  深南大道  阅读(62368)  评论(0编辑  收藏  举报