/**
* 生成一个防抖函数
* @param func 执行函数
* @param wait 等待时间
* @param immediate 是否立即执行
*/
export function debounce(func: any, wait: number = 800, immediate = false) {
let timer: any = '';
return function (args) {
const _this = this;
const _arguments = arguments;
let res;
if (timer) clearTimeout(timer);
if (immediate) {
let nowDo = !timer;
timer = setTimeout(function () {
timer = null;
}, wait);
if (nowDo) res = func.call(_this, ..._arguments);
} else {
timer = setTimeout(function () {
res = func.call(_this, ..._arguments);
}, wait);
}
return res;
};
}
网友评论