@naverpay/hidash
Version:
improved lodash
32 lines (30 loc) • 1.72 kB
text/typescript
interface DebounceOptions {
leading?: boolean;
maxWait?: number;
trailing?: boolean;
}
/**
* @description
* Creates a debounced function that delays invoking `func` until after `waitMilliseconds`
* have elapsed since the last time the debounced function was invoked.
* The debounced function comes with a `cancel` method to cancel delayed `func`
* invocations and a `flush` method to immediately invoke them. Provide `options`
* to indicate whether `func` should be invoked on the leading and/or trailing edge of
* the `waitMilliseconds` timeout. The `func` is invoked with the last arguments provided to the
* debounced function. Subsequent calls to the debounced function return the result of
* the last `func` invocation.
*
* @param {Function} func - The function to debounce.
* @param {number} [waitMilliseconds=0] - The number of milliseconds to delay.
* @param {Object} [options={}] - The options object.
* @param {boolean} [options.leading=false] - Specify invoking on the leading edge of the timeout.
* @param {number} [options.maxWait] - The maximum time `func` is allowed to be delayed before it's invoked.
* @param {boolean} [options.trailing=true] - Specify invoking on the trailing edge of the timeout.
* @returns {{debounce: Function, cancel: Function, flush: Function}} An object containing the debounced function, a cancel function, and a flush function.
*/
declare function debounce<Args extends unknown[]>(func: (...args: Args) => unknown, waitMilliseconds?: number, { leading, trailing, maxWait }?: DebounceOptions): {
debounce: (this: unknown, ...args: Args) => unknown;
cancel: () => void;
flush: () => unknown;
};
export { debounce, debounce as default };