函数防抖和节流
目录
防抖(debounce)
防抖函数
const debounce = (fn, delay) => {
let timer = null
return function (...args) {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
}
节流(throttle)
节流函数
const throttle = (fn, delay) => {
let timer = null
return function (...args) {
if (!timer) {
fn.apply(this, args)
timer = setTimeout(() => {
timer = null
}, delay)
}
}
}
对你有帮助?请作者喝杯咖啡~
支付宝
微信