devscard/src/web/utils/throttle.ts
2023-01-20 16:13:03 +01:00

24 lines
588 B
TypeScript

const throttle = (fn: () => void, wait: number) => {
let initialized: boolean;
let timeoutId: NodeJS.Timeout;
let lastCalled: number;
const isPastTimeout = () => Date.now() - lastCalled >= wait;
const getTimeout = () => Math.max(wait - (Date.now() - lastCalled), 0);
const call = () => {
fn();
initialized ??= true;
lastCalled = Date.now();
};
const recall = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => isPastTimeout() && call(), getTimeout());
};
return () => (initialized ? recall() : call());
};
export default throttle;