@beeapi/nutui
Version:
一套轻量级移动端Vue组件库
33 lines (31 loc) • 728 B
JavaScript
/**
* @desc 函数节流
* @param func 函数
* @param wait 延迟执行毫秒数
* @param type 1 表时间戳版,2 表定时器版
*/
export const throttle = (func, wait, type) => {
if (type === 1) {
var previous = 0;
} else if (type === 2) {
var 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);
}
}
};
};