super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
32 lines (31 loc) • 1.13 kB
TypeScript
import { ThrottleOptions } from '../utils/types';
/**
* Creates a throttled function that only invokes func at most once per
* every wait milliseconds. The throttled function comes with a cancel
* method to cancel delayed func invocations and a flush method to
* immediately invoke them.
*
* @param func - The function to throttle
* @param wait - The number of milliseconds to throttle invocations to
* @param options - The options object
* @returns The new throttled function
*
* @example
* ```ts
* // Avoid excessively updating the position while scrolling
* window.addEventListener('scroll', throttle(updatePosition, 100));
*
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes
* const throttled = throttle(renewToken, 300000, {
* trailing: false
* });
* element.addEventListener('click', throttled);
*
* // Cancel the trailing throttled invocation
* throttled.cancel();
* ```
*/
export declare function throttle<T extends (...args: any[]) => any>(func: T, wait?: number, options?: ThrottleOptions): T & {
cancel: () => void;
flush: () => void;
};