Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 8x 8x 1x 1x 1x 100x 100x 1x 1x 99x 99x 99x | import { purry } from "./purry";
/**
* The Debounce technique allow us to “group” multiple sequential calls in a single one.
* @description
* https://css-tricks.com/debouncing-throttling-explained-examples/
* @param func - Any provided function
* @param debounceTimeMs - duration in milliseconds
* @example
* const debouncedLog = P.debounce(console.log, 500)
* debouncedLog("I will be printed only if 500ms ago this function was not called")
* @category Function
*/
export function debounce<E extends (...args: any[]) => any>(debounceTimeMs: number): (func: E) => E
export function debounce<E extends (...args: any[]) => any>(func: E, debounceTimeMs: number): E
export function debounce() {
return purry(__debounce, arguments)
}
function __debounce<E extends (...args: any[]) => any>(
func: E,
debounceTimeMs: number
): E {
// tslint:disable: no-let
let result: { readonly r: ReturnType<E> } | null = null;
let debounceTimer: any = null;
return ((...args) => {
if (result == null) {
result = {
r: func(...args),
};
return result.r;
}
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
result = {
r: func(...args),
};
}, debounceTimeMs);
return result.r;
}) as E;
}
|