UNPKG

mauss

Version:

lightweight, modular, type-safe utilities

65 lines (64 loc) 1.35 kB
const DURATION = 300; /** * Prevent execution of `fn` until `time` has passed * * @example * * ```js * function update(name) {...} // can also be inlined * * const search = debounce(update, 500); * * search('mauss'); // execute after 500ms * ``` */ export function debounce(fn, time = DURATION) { let timeout; return (...args) => { if (timeout) clearTimeout(timeout); timeout = setTimeout(() => fn(...args), time); }; } /** * Immediately execute `fn` and prevent the next execution until after `time` * * @example * * ```js * function update(name) {...} * * const onclick = immediate(update, 500); * ``` */ export function immediate(fn, time = DURATION) { let timeout; return (...args) => { if (timeout) return; fn(...args); timeout = setTimeout(() => clearTimeout(timeout), time); }; } /** * Prevent executions after the first `fn` until `time` has passed * * @example * * ```js * function update(name) {...} * * const search = throttle(update, 500); * * search('mauss'); // execute every 500ms * ``` */ export function throttle(fn, time = DURATION) { let wait = false; return (...args) => { if (wait) return; fn(...args), (wait = true); setTimeout(() => (wait = false), time); }; }