Skip to main content

Function Methods

debounce

Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked.

The debounced function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them.

Since: 0.1.0

Arguments

ParamTypeDescription
funcFunctionThe function to debounce.
waitnumberThe number of milliseconds to delay
[immediate]booleanIf immediate is true, the argument func will be triggered at the beginning of the sequence instead of at the end.

Returns

TypeDescription
FunctionReturns debounce function. You can cancel debounce using debounced.cancel

Declaration

declare function debounce<
T extends (...args: any[]) => any = (...args: any[]) => any,
>(func: T, wait: number, immediate?: boolean): Debounced<T>;

type Debounced<T extends (...args: T[]) => any> = {
(...args: Parameters<T>): ReturnType<T>;
cancel: () => void;
};

Examples

const debounced = debounce(resizeHandler, 200);
document.body.addEventListener('resize', debounced);

// You can cancel the trailing bebounced invocation.
function mustStopDebounce() {
debounced.cancel();
}

// Invoke `requestData` when clicked, debouncing subsequent calls.
$requestApiBtn.addEventListener('click', debounce(requestData, 300, true));

sleep

Trigger delay

Since: 0.1.0

Arguments

ParamTypeDescription
waitnumber The number of milliseconds to delay

Declaration

declare const sleep: (wait: number) => Promise<void>;

throttle

Creates a throttled function that only invokes func at most once per every wait milliseconds.

The throttled function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them.

Since: 0.1.0

Arguments

ParamTypeDescription
funcFunctionThe function to throttle.
waitnumberThe number of milliseconds to delay
[options = {}]ThrottleOptionsThe options object.
[options.leading = true]booleanSpecify invoking on the leading edge of the timeout.
[options.trailing = true]booleanSpecify invoking on the trailing edge of the timeout.

Returns

TypeDescription
FunctionReturns throttled function. You can cancel throttle using throttled.cancel

Declaration

declare function throttle<T extends (...args: any[]) => any>(
func: T,
wait: number,
options?: { leading?: boolean; trailing?: boolean }
): Throttled<T>;

type Throttled<T extends (...args: T[]) => any> = {
(...args: Parameters<T>): ReturnType<T>;
cancel: () => void;
};

Examples

const throttled = throttle(scrollHandler, 200);
window.addEventListener('scroll', throttled);

// You can cancel the trailing throttled invocation.
throttled.cancel();

times

Invokes the iteratee n times, returning an array of the results of each invocation.

The iteratee is invoked with one argument; (index).

Since: 1.1.0

Arguments

ParamTypeDescription
countnumberThe number of times to invoke iteratee.
fnFunctionThe function invoked per iteration.

Returns

TypeDescription
any[]Returns the array of results.

Declaration

declare function times<Fn extends (...args: any[]) => any>(
count: number,
fn: Fn
): ReturnType<Fn>[];

Examples

times(3, Boolean); // [false, true, true]