/**
* 防抖函数
* @param {*} fn
* @param {*} wait
* @returns
*/
export const Debounce = (fn, wait = 500) => {
let timer
return function () {
const args = arguments
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
timer = null
fn.apply(this, args)
}, wait)
}
}
/**
* 节流函数
* @param {*} fn
* @param {*} wait
* @returns
*/
export const Throttle = (fn, wait = 3000) => {
let last, deferTimer
return function () {
var _args = arguments
var _this = this
var now = +new Date()
clearTimeout(deferTimer)
if (last && now < last + wait) {
deferTimer = setTimeout(function () {
last = now
fn.apply(_this, _args)
}, wait)
} else {
last = now
fn.apply(_this, _args)
}
}
}