From c8a38cdd8301811254d72a02faeadf1a9f4bcf8c Mon Sep 17 00:00:00 2001 From: Mikkel Date: Tue, 7 Feb 2023 14:18:00 +0100 Subject: [PATCH] utils.ts -> Throttler.ts --- frontend/src/Throttler.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 frontend/src/Throttler.ts 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); + } +}