Defined in: throttler.ts:150
A class that creates a throttled function.
Throttling ensures a function is called at most once within a specified time window. Unlike debouncing which waits for a pause in calls, throttling guarantees consistent execution timing regardless of call frequency. This synchronous version is lighter weight and often all you need - upgrade to AsyncThrottler when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
Supports both leading and trailing edge execution:
For collapsing rapid-fire events where you only care about the last call, consider using Debouncer.
State Management:
const throttler = new Throttler(
(id: string) => api.getData(id),
{ wait: 1000 } // Execute at most once per second
);
// First call executes immediately
throttler.maybeExecute('123');
// Subsequent calls within 1000ms are throttled
throttler.maybeExecute('123'); // Throttled
const throttler = new Throttler(
(id: string) => api.getData(id),
{ wait: 1000 } // Execute at most once per second
);
// First call executes immediately
throttler.maybeExecute('123');
// Subsequent calls within 1000ms are throttled
throttler.maybeExecute('123'); // Throttled
• TFn extends AnyFunction
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
Defined in: throttler.ts:158
TFn
ThrottlerOptions<TFn>
Throttler<TFn>
fn: TFn;
fn: TFn;
Defined in: throttler.ts:159
key: undefined | string;
key: undefined | string;
Defined in: throttler.ts:154
options: ThrottlerOptions<TFn>;
options: ThrottlerOptions<TFn>;
Defined in: throttler.ts:155
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
Defined in: throttler.ts:151
_emit(): void
_emit(): void
Defined in: throttler.ts:179
Emits a change event for the throttler instance. Mostly useful for devtools.
void
cancel(): void
cancel(): void
Defined in: throttler.ts:323
Cancels any pending trailing execution and clears internal state.
If a trailing execution is scheduled (due to throttling with trailing=true), this will prevent that execution from occurring. The internal timeout and stored arguments will be cleared.
Has no effect if there is no pending execution.
void
flush(): void
flush(): void
Defined in: throttler.ts:301
Processes the current pending execution immediately
void
maybeExecute(...args): void
maybeExecute(...args): void
Defined in: throttler.ts:242
Attempts to execute the throttled function. The execution behavior depends on the throttler options:
If enough time has passed since the last execution (>= wait period):
If within the wait period:
...Parameters<TFn>
void
const throttled = new Throttler(fn, { wait: 1000 });
// First call executes immediately
throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
throttled.maybeExecute('c', 'd');
const throttled = new Throttler(fn, { wait: 1000 });
// First call executes immediately
throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
throttled.maybeExecute('c', 'd');
reset(): void
reset(): void
Defined in: throttler.ts:334
Resets the throttler state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: throttler.ts:184
Updates the throttler options
Partial<ThrottlerOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
