function throttle<TFn>(fn, initialOptions): (...args) => void
function throttle<TFn>(fn, initialOptions): (...args) => void
Defined in: throttler.ts:216
Creates a throttled function that limits how often the provided function can execute.
Throttling ensures a function executes at most once within a specified time window, regardless of how many times it is called. This is useful for rate-limiting expensive operations or UI updates.
The throttled function can be configured to execute on the leading and/or trailing edge of the throttle window via options.
For handling bursts of events, consider using debounce() instead. For hard execution limits, consider using rateLimit().
• TFn extends (...args) => any
TFn
Omit<ThrottlerOptions, "enabled">
Function
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
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');
// Basic throttling - max once per second
const throttled = throttle(updateUI, { wait: 1000 });
// Configure leading/trailing execution
const throttled = throttle(saveData, {
wait: 2000,
leading: true, // Execute immediately on first call
trailing: true // Execute again after delay if called during wait
});
// Basic throttling - max once per second
const throttled = throttle(updateUI, { wait: 1000 });
// Configure leading/trailing execution
const throttled = throttle(saveData, {
wait: 2000,
leading: true, // Execute immediately on first call
trailing: true // Execute again after delay if called during wait
});
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.