Framework
Version
防抖器 API 參考
節流器 API 參考
速率限制器 API 參考
佇列 API 參考

Throttler

Class: Throttler<TFn>

Defined in: throttler.ts:67

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.

Supports both leading and trailing edge execution:

  • Leading: Execute immediately on first call (default: true)
  • Trailing: Execute after wait period if called during throttle (default: true)

For collapsing rapid-fire events where you only care about the last call, consider using Debouncer.

Example

ts
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

Type Parameters

TFn extends AnyFunction

Constructors

new Throttler()

ts
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>
new Throttler<TFn>(fn, initialOptions): Throttler<TFn>

Defined in: throttler.ts:74

Parameters

fn

TFn

initialOptions

ThrottlerOptions<TFn>

Returns

Throttler<TFn>

Methods

cancel()

ts
cancel(): void
cancel(): void

Defined in: throttler.ts:171

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.

Returns

void


getExecutionCount()

ts
getExecutionCount(): number
getExecutionCount(): number

Defined in: throttler.ts:196

Returns the number of times the function has been executed

Returns

number


getIsPending()

ts
getIsPending(): boolean
getIsPending(): boolean

Defined in: throttler.ts:203

Returns true if there is a pending execution

Returns

boolean


getLastExecutionTime()

ts
getLastExecutionTime(): number
getLastExecutionTime(): number

Defined in: throttler.ts:182

Returns the last execution time

Returns

number


getNextExecutionTime()

ts
getNextExecutionTime(): number
getNextExecutionTime(): number

Defined in: throttler.ts:189

Returns the next execution time

Returns

number


getOptions()

ts
getOptions(): Required<ThrottlerOptions<TFn>>
getOptions(): Required<ThrottlerOptions<TFn>>

Defined in: throttler.ts:100

Returns the current throttler options

Returns

Required<ThrottlerOptions<TFn>>


maybeExecute()

ts
maybeExecute(...args): void
maybeExecute(...args): void

Defined in: throttler.ts:126

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):

    • With leading=true: Executes immediately
    • With leading=false: Waits for the next trailing execution
  • If within the wait period:

    • With trailing=true: Schedules execution for end of wait period
    • With trailing=false: Drops the execution

Parameters

args

...Parameters<TFn>

Returns

void

Example

ts
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');

setOptions()

ts
setOptions(newOptions): void
setOptions(newOptions): void

Defined in: throttler.ts:88

Updates the throttler options Returns the new options state

Parameters

newOptions

Partial<ThrottlerOptions<TFn>>

Returns

void

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.