JS執行一次任務與按期任務與清除執行

 

1.一次性任務的執行與清除執行

1.按期執行

<script> timer = 0; timer = setTimeout(function() { console.log("setTimeout---" + timer) }, 1 * 1000) </script>

 

效果:spa

 

2.清除按期執行

<script> timer = 0; timer = setTimeout(function() { console.log("setTimeout---" + timer) }, 1 * 1000)  clearTimeout(timer); </script>

 

結果控制檯不會打印上面的信息code

 

 

2.週期性執行任務與清除

1.執行

<script> timer = 0; timer = setInterval(function() { console.log("setTimeout---" + timer) }, 2 * 1000) </script>

 

結果:blog

 

 2.清除效果:

<script> timer = 0; timer = setInterval(function() {   console.log("setTimeout---" + timer) }, 2 * 1000) clearInterval(timer); </script>

 

 

結果控制檯不會打印信息ip

相關文章
相關標籤/搜索