front-standard-utils
Version:
19 lines (18 loc) • 472 B
text/typescript
/***
* 节流函数
* @param fn 事件触发的操作
* @param delay 可选,多少毫秒内连续触发事件不会执行,默认200毫秒
* @returns {Function}
*/
export const throttle = (fn: Function, delay: number = 200) => {
let flag = true;
return function (this: any) {
if (!flag) return;
let this_: any = this, args: any = arguments
flag = false;
setTimeout(() => {
fn.apply(this_, args);
flag = true;
}, delay);
};
};