The throttle function returns a throttled version of a function that, when invoked repeatedly, will only call the original function at most once every ms milliseconds.
The throttle function is used to limit the number of times a function is called within a short period of time, such as scroll events. This can significantly improve performance, especially if the function is expensive or computationally heavy.
Usage
import { throttle } from'@mustib/utils';
const fn = throttle(() => {
console.log('throttled')
}, 100)
fn();fn();fn(); // logs "throttled" once
Definition
functionthrottle<TextendsFunc=Func>(
func:T,
ms=100
):T { }
Parameters
func
type func =Function;
The function to throttle.
ms
type ms =number;
/* default value */
const ms = 100;
The number of milliseconds to throttle invocations to.