diff --git a/frontend/src/Throttler.ts b/frontend/src/Throttler.ts new file mode 100644 index 0000000..c55e284 --- /dev/null +++ b/frontend/src/Throttler.ts @@ -0,0 +1,17 @@ +export class Throttler { + private hasBeenCalledWithinTime = false; + private lastCallFunc: (() => any) | null = null; + + public constructor(private minimumTimeBetweenCall: number) {} + + public call(func: () => any) { + this.lastCallFunc = func; + if (this.hasBeenCalledWithinTime) return; + this.hasBeenCalledWithinTime = true; + func(); + setTimeout(() => { + this.hasBeenCalledWithinTime = false; + if (this.lastCallFunc) this.lastCallFunc(); + }, this.minimumTimeBetweenCall); + } +}