30 lines
669 B
JavaScript
30 lines
669 B
JavaScript
/**
|
|
* @desc 函数节流
|
|
* @param func 函数
|
|
* @param wait 延迟执行毫秒数
|
|
* @param type 1 使用表时间戳,在时间段开始的时候触发 2 使用表定时器,在时间段结束的时候触发
|
|
*/
|
|
export const throttle = (func, wait = 1000, type = 1) => {
|
|
let previous = 0;
|
|
let timeout;
|
|
return function() {
|
|
let context = this;
|
|
let args = arguments;
|
|
if (type === 1) {
|
|
let now = Date.now();
|
|
|
|
if (now - previous > wait) {
|
|
func.apply(context, args);
|
|
previous = now;
|
|
}
|
|
} else if (type === 2) {
|
|
if (!timeout) {
|
|
timeout = setTimeout(() => {
|
|
timeout = null;
|
|
func.apply(context, args)
|
|
}, wait)
|
|
}
|
|
}
|
|
}
|
|
}
|