mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +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;
|
let timer: NodeJS.Timeout;
|
||||||
return (...args: Array<any>) => {
|
return (...args: Array<any>) => {
|
||||||
|
if (timer === undefined && leading) {
|
||||||
|
fn.apply(this, args);
|
||||||
|
}
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
fn.apply(this, args);
|
fn.apply(this, args);
|
||||||
|
timer = undefined;
|
||||||
}, delay);
|
}, 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 fn Function to debounce
|
||||||
* @param delay Customize the delay (default is 300ms)
|
* @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
|
* Releases the reference to the wrapped native object
|
||||||
|
Reference in New Issue
Block a user