feat: leading support for debounce (#10388)

This commit is contained in:
farfromrefuge
2023-09-28 02:06:36 +00:00
committed by GitHub
parent 6d44c2d6e0
commit b6a5250511
2 changed files with 6 additions and 2 deletions

View File

@ -173,12 +173,16 @@ export function mainThreadify(func: Function): (...args: any[]) => void {
};
}
export function debounce(fn: any, delay = 300) {
export function debounce(fn: any, delay = 300, { leading }: { leading?: boolean } = {}) {
let timer: NodeJS.Timeout;
return (...args: Array<any>) => {
if (timer === undefined && leading) {
fn.apply(this, args);
}
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
timer = undefined;
}, delay);
};
}

View File

@ -45,7 +45,7 @@ export function throttle(fn: any, delay?: number);
* @param fn Function to debounce
* @param delay Customize the delay (default is 300ms)
*/
export function debounce(fn: any, delay?: number);
export function debounce(fn: any, delay?: number, options?: { leading?: boolean });
/**
* Releases the reference to the wrapped native object