防抖
function debound(fn, delay) {
let timer = null
return function () {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, arguments)
}, delay)
}
}
节流(三种)
function throttle(fn, delay) {
let cd = 0
return function () {
let now = Date.now()
if (now - cd > delay) {
fn.apply(this, arguments)
cd = now
}
}
}
function throttle1(fn, delay) {
let cd = true
return function () {
if (cd) {
fn.apply(this, arguments)
cd = false
setTimeout(() => (cd = true), delay)
}
}
}
function throttle2(fn, delay) {
let timer = null
return function () {
if (timer) return
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}