//防抖
function debounce(fn, delay = 500) {
// timer 是闭包中的
let timer = null
return (...args) => {
clearTimeout(timer)
timer = setTimeout(() => {
fn(...args)
}, delay)
}
}
// 节流
function throttle(fn, delay = 100) {
let timer = null
return (...args) => {
if (timer) {
return
}
timer = setTimeout(() => {
fn(...args)
timer = null
}, delay)
}
}