@gvray/eskit
Version:
A rich and colorful toolkit about typescript and javascript.
44 lines • 1.59 kB
TypeScript
export interface DebouncedFunction<TArgs extends any[]> {
(...args: TArgs): void;
cancel(): void;
flush(): void;
pending(): boolean;
}
/**
* Creates a debounced function that waits for the specified delay after the last call before executing.
* 创建一个防抖函数,在最后一次调用后等待指定延迟时间再执行。
*
* @param fn The function to wrap. / 要包装的函数
* @param delay The delay time (in milliseconds) before the function is executed. / 函数执行前的延迟时间(毫秒)
* @param immediate Whether to execute the function immediately on the first call. / 是否在第一次调用时立即执行函数
* @returns The wrapped debounced function. / 包装后的防抖函数
*
* @example
* ```typescript
* // Basic debounce
* const debouncedFn = debounce(() => {
* console.log('Called after delay!');
* }, 1000);
*
* // With immediate execution
* const immediateDebounced = debounce(() => {
* console.log('Called immediately!');
* }, 1000, true);
*
* // Search input example
* const searchDebounced = debounce((query: string) => {
* performSearch(query);
* }, 300);
*
* // Cancel if needed
* searchDebounced('hello');
* searchDebounced.cancel(); // Cancels the pending call
*
* // Force execution
* searchDebounced('world');
* searchDebounced.flush(); // Executes immediately
* ```
*/
export declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, delay: number, immediate?: boolean): DebouncedFunction<TArgs>;
export default debounce;
//# sourceMappingURL=debounced.d.ts.map