防抖节流
防抖
export const debounce = (() => {
let timer = null
return (callback, wait = 200) => {
timer&&clearTimeout(timer)
timer = setTimeout(callback, wait)
}
})()
// vue2
methods: {
loadList() {
debounce(() => {
console.log('加载数据')
}, 500)
}
}
节流
export const throttle = (() => {
let last = 0
return (callback, wait = 200) => {
let now = +new Date()
if (now - last > wait) {
callback()
last = now
}
}
})()