es-toolkit/docs/reference/function/throttle.md
Jungwoo LEE 79046ea5c1
feat(debounce, throttle): Support passing arguments to callback function in debounce & throttle function (#26)
* feat: add support for variable arguments in debounce.ts

* feat: add support for variable arguments in throttle.ts

* Update src/function/debounce.ts

* Apply suggestions from code review

* Update src/function/debounce.ts

* Update src/function/throttle.ts

* docs: update related docs

---------

Co-authored-by: Sojin Park <raon0211@gmail.com>
Co-authored-by: Sojin Park <raon0211@toss.im>
2024-06-05 08:05:56 +09:00

929 B

throttle

Creates a throttled function that only invokes the provided function at most once per every throttleMs milliseconds. Subsequent calls to the throttled function within the wait time will not trigger the execution of the original function.

Signature

function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): F;

Parameters

  • func (F): The function to throttle.
  • throttleMs(number): The number of milliseconds to throttle executions to.

Returns

(F): A new throttled function.

Examples

const throttledFunction = throttle(() => {
  console.log('Function executed');
}, 1000);

// Will log 'Function executed' immediately
throttledFunction();

// Will not log anything as it is within the throttle time
throttledFunction();

// After 1 second
setTimeout(() => {
  throttledFunction(); // Will log 'Function executed'
}, 1000);