Throttle & Debounce Utils
/**
* Throttle: fire at most once per `limit` ms
*/
export function throttle(fn, limit = 200) {
let inThrottle = false;
return function (...args) {
if (!inThrottle) {
fn.apply(this, args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}
/**
* Debounce: fire only after `delay` ms of silence
*/
export function debounce(fn, delay = 300) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
};
}