Skip to content

throttle

type: function
since: v2.6.0

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

function throttle<T extends Func = Func>(
func: T,
ms = 100
): T { }

Parameters

  1. func
    type func = Function;
    • The function to throttle.
  2. ms
    type ms = number;
    /* default value */
    const ms = 100;
    • The number of milliseconds to throttle invocations to.

Returns

type T = Function;
  • Throttled version of the input function