封装
let timer = null //防抖
export function debounce(fn, delay) {
return (function (...args) {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
})()
}
let last = 0 // 节流
export function throttle(fn, delay) {
return (function (...args) {
const now = Date.now()
if (now - last > delay) {
last = now
fn.apply(this, args)
}
})()
}
使用
wrr() {
debounce(() => {
console.log(555);
}, 1000);
},
xss() {
throttle(() => {
console.log(666);
}, 1000);
},