General API

rateLimit()

Limits the frequency at which a function is called using throttling or debouncing.

  • Accepts a function as the first argument.
    Accepts a number as the delay time for the second argument.
    Accepts an optional third argument as a boolean value (true or false):
    Argument
    Description
    true
    Uses throttle to restrict a function from being executed at most once in a specified timeframe.
    For example, with a 500ms delay, the function can be called every 500ms but not more frequently than that.
    false
    Uses debounce to ensure that a function is executed only after a specified period of inactivity.
    For example, with a 500ms delay, it will run once after 500ms of no input.
    This is the default value.
Example:
window.evt.add('scroll', rateLimit(() => {...}, 500));
// Adds a scroll event with a 500-millisecond debounce rate limit.

$('input').evt.add('input', rateLimit((e) => {...}, 500, true));
// Adds an input event with a 500-millisecond throttle rate limit.

const fetchData = rateLimit(async (e, data) => {...}, 500);
// Assigns an asynchronous function with a 500-millisecond debounce rate limit to a variable.

export default rateLimit((e, data) => {...}, 500, true);
// The calling of the exported module function with a 500-millisecond throttle rate limit.