1.基本概念
针对事件高频触发的两种解决策略。
防抖(debounce):在n秒内触发,则重新计时,直至最后一次触发,隔n秒后执行事件
节流(throttle):每隔n秒触发一次
2.适用场景
防抖:input
节流:scroll,resize
3.代码
//防抖
function debounce(func,time){
var timeout=null
return function(){
if(timeout!==null){
clearTimeout(timeout)
}
timeout=setTimeout(func,time)
}
}
//节流
function throttle(func,time1){
let time=true;
return function(){
if(time===false){
return
}
time=false
setTimeout(()=>{
func.apply(this,arguments)
time=true
},time1)
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。