mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
932 B
932 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(func: () => void, throttleMs: number): () => void;
Parameters
func
(() => void
): The function to throttle.throttleMs
(number
): The number of milliseconds to throttle executions to.
Returns
(() => void
): 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);