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
Param | Type | Description |
---|---|---|
func | Function | The function to debounce. |
wait | number | The number of milliseconds to delay |
[immediate] | boolean | If immediate is true , the argument func will be triggered at the beginning of the sequence instead of at the end. |
Returns
Type | Description |
---|---|
Function | Returns 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
Param | Type | Description |
---|---|---|
wait | number | 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
Param | Type | Description |
---|---|---|
func | Function | The function to throttle. |
wait | number | The number of milliseconds to delay |
[options = {}] | ThrottleOptions | The options object. |
[options.leading = true] | boolean | Specify invoking on the leading edge of the timeout. |
[options.trailing = true] | boolean | Specify invoking on the trailing edge of the timeout. |
Returns
Type | Description |
---|---|
Function | Returns 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
Param | Type | Description |
---|---|---|
count | number | The number of times to invoke iteratee. |
fn | Function | The function invoked per iteration. |
Returns
Type | Description |
---|---|
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]