//防抖
function debounce(fn, delay = 500) {
let timer;
return function () {
if (timer) {
clearTimeout(timer)
}
const args = arguments
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
}
//节流
function throttle(fn, delay = 200) {
let flag = true
return function () {
if (!flag) return
flag = false
const args = arguments
setTimeout(() => {
fn.apply(this, args)
flag = true
}, delay)
}
}
js 实现节流 防抖函数
最新推荐文章于 2022-08-09 12:00:25 发布