Skip to content

Commit

Permalink
feat(core): debounce throttle tool
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-fang committed May 14, 2023
1 parent db89b88 commit c9bf1b3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/core/function/tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 函数节流(每隔一段时间执行一次,防止函数过于频繁调用,导致性能问题)
* @param {Function} fn
* @param {number} wait 时间(单位:ms)
* @returns 节流函数
*/
export const throttle = (fn: Function, wait: number = 300) => {
let timer = false;
return (...args: any[]) => {
if (timer) return;
timer = true;
setTimeout(() => {
fn(...args);
timer = false;
}, wait);
};
};
/**
* 函数防抖(保证一个函数在多少毫秒内不被再次触发,只会执行一次)
* @param fn 将要处理的函数
* @param wait 事件(单位:ms)
* @param immediate 是否在触发事件后,在时间段n开始,立即执行;否则在时间段n结束,才执行。
* @returns 防抖函数
*/
export const debounce = (fn: Function, wait: number, immediate: boolean = false) => {
let timer: any = null;
return (this: unknown, ...args: any) => {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (immediate) {
if (!timer) fn.apply(this, args); // 第一次调用时执行
timer = setTimeout(() => {
// n秒内多次触发事件,重新计算timer
timer = null; // n秒内没有触发事件,timer设置为null,保证n秒后能重新触发事件
}, wait);
} else {
timer = setTimeout(() => {
// 延迟指定毫秒后调用
fn.apply(this, args);
}, wait);
}
};
};

0 comments on commit c9bf1b3

Please sign in to comment.