当页面刷新比较频繁或者多次调用同一语句时,推荐使用防抖函数或者节流函数来进行调用。下面是防抖函数的代码。
debounce(fn, time){
let timer = null
return function(...args) {
if(timer){
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, args)
}, time)
}
当页面刷新比较频繁或者多次调用同一语句时,推荐使用防抖函数或者节流函数来进行调用。下面是防抖函数的代码。
debounce(fn, time){
let timer = null
return function(...args) {
if(timer){
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, args)
}, time)
}