/*
*函数防抖 如果事件被触发,清除timer并重新开始计时
*/
export function _debounce(fn, time) {
let timer = null
return function() {
if (!timer) {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, time)
}
}
}
/*
- 函数节流 规定时间内按钮只执行一次
*/
export function throttle(fn, gapTime) {
if (gapTime === null || gapTime === undefined) {
gapTime = 1500
}
let _lastTime = null
// 返回新的函数
return function () {
let _nowTime = + new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(this, arguments) // 将this和参数传给原函数
_lastTime = _nowTime
}
}
}