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>
联系客服