mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 10:01:08 +08:00
feat: leading support for debounce (#10388)
This commit is contained in:
@ -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);
|
||||
};
|
||||
}
|
||||
|
2
packages/core/utils/index.d.ts
vendored
2
packages/core/utils/index.d.ts
vendored
@ -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
|
||||
|
Reference in New Issue
Block a user