1.循环执行:
var timeid = window.setInterval(“方法名或方法”,“延时”);
window.clearInterval(timeid);
1 <script type="text/javascript"> 2 $(document).ready(function(){ 3 //循环执行,每隔1秒钟执行一次 1000 4 var t1=window.setInterval(refreshCount, 1000); 5 function refreshCount() { 6 console.log("ready"); 7 } 8 //去掉定时器的方法 9 window.clearInterval(t1); 10 }); 11 </script>
2.定时执行:当方法执行完成定时器停止(但是定时器还在,只不过没用了);
var tmid = window.setTimeout(“方法名或方法”, “延时”);
window.clearTimeout(tmid);
1 <script type="text/javascript"> 2 $(document).ready(function(){ 3 //定时执行,5秒后执行 4 var t1=window.setTimeout(refreshCount, 1000 * 5); 5 function refreshCount() { 6 console.log("ready"); 7 } 8 //去掉定时器的方法 9 window.clearTimeout(t1); 10 }); 11 </script>
转自:https://www.cnblogs.com/Dream2hc/p/web23863.html