// 防抖和节流
// 使用方法:
// let exportFunct = throttle(this.exportExcel,5000)
// exportFunct(传参数)
// 如果没有参数:exportFunct()
export function debounce(fn, wait) {
return function(args) {
//获取函数的作用域和变量
let that = this
let _args = args
//每次事件被触发,都会清除当前的timer,然后重写设置超时调用
clearTimeout(fn.id)
fn.id = setTimeout(function() {
fn.call(that, _args)
}, wait)
}
}
export function throttle(fn, wait, delay) {
let last,
deferTimer = null
return function() {
let that = this
let _args = arguments
let now = +new Date()
if (last && now < last + delay) {
clearTimeout(deferTimer)
deferTimer = setTimeout(function() {
last = now
fn.apply(that, _args)
}, wait)
} else {
console.log(32, 3222)
last = now
fn.apply(that, _args)
}
}
}
防抖和节流
最新推荐文章于 2025-11-26 17:11:00 发布
800

被折叠的 条评论
为什么被折叠?



