节流、防抖
// 节流
function throttle(fn, delay, asThis) {
let timer = null;
return (...args) => {
if (timer) return
fn.call(asThis, ...args)
timer = setTimeout(() => {
timer = false;
}, delay)
}
}
// 防抖
function debounce(fn, delay, asThis) {
let timer = null;
return (...args) => {
timer && clearTimeout(timer)
timer = setTimeout(() => {
fn.call(asThis, ...args)
timer = null;
}, delay)
}
}