2024-04-25 14:56:13 +03:00
|
|
|
# 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
|
|
|
|
|
|
|
|
```typescript
|
2024-06-05 02:05:56 +03:00
|
|
|
function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): F;
|
2024-04-25 14:56:13 +03:00
|
|
|
```
|
|
|
|
|
2024-06-04 11:19:26 +03:00
|
|
|
### Parameters
|
2024-04-25 14:56:13 +03:00
|
|
|
|
2024-06-05 02:05:56 +03:00
|
|
|
- `func` (`F`): The function to throttle.
|
2024-04-25 14:56:13 +03:00
|
|
|
- `throttleMs`(`number`): The number of milliseconds to throttle executions to.
|
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
2024-06-05 02:05:56 +03:00
|
|
|
(`F`): A new throttled function.
|
2024-04-25 14:56:13 +03:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
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);
|
|
|
|
```
|