防抖节流
防抖
频繁点击下,只执行一次
一般用于点击事件
非立即执行的防抖
只执行最后一个
//非立即执行的防抖
//func是个函数,wait是时间
function debounce(func, wait) {
let timer;
return function () {
let that = this;
clearTimeout(timer);
timer = null;
timer = setTimeout(function () {
res = func.apply(that)
}, wait)
}
}
box.onclick = debounce(fn, 1000);
立即执行的防抖
只执行第一个
function debounce(func,wait){
let timer;
return function(){
clearTimeout(timer)