@nodecg/types
Version:
Dynamic broadcast graphics rendered in a browser
30 lines (27 loc) • 532 B
text/typescript
const timers = new Map<string, NodeJS.Timer>();
const queued = new Set<string>();
/**
* A standard throttle, but uses a string `name` as the key instead of the callback.
*/
export function throttleName(
name: string,
callback: () => void,
duration = 500,
): void {
const existing = timers.get(name);
if (existing) {
queued.add(name);
return;
}
callback();
timers.set(
name,
setTimeout(() => {
timers.delete(name);
if (queued.has(name)) {
queued.delete(name);
callback();
}
}, duration),
);
}