1. setTimeout 超时调用
特点:间歇一段时间,只会触发一次
setTimeout(function(){console.log("hello");},1000)
//使用递归 让超时调用实现间歇调用function show(){setTimeout(function(){console.log(1);show();},1000)}show();
2. setInterval 间歇调用
特点:每间隔一段时间就会触发
setInterval(function(){console.log("world");},2000)
3. 清除定时器
定时器会有一个id值,记录它在内存中的位置如果想清除定时器,只需要使用clearInterval()方法,清除这个id值就可以了clearTimeout()
<button id="clear">清除定时器</button><script>/*** setTimeout 间隔一定的时间触发,并且只会触发一次* setInterval 间隔一定的时间重复触发*/var clear = document.getElementById("clear")var timer = setInterval(function(){console.log('wow');},1000)clear.onclick = function(){clearInterval(timer)}/*** clearTimeout setTimeout* clearInterval setInterval*/</script>
