utils.ts -> Throttler.ts

This commit is contained in:
Mikkel 2023-02-07 14:18:00 +01:00
parent 20de548763
commit c8a38cdd83

17
frontend/src/Throttler.ts Normal file
View File

@ -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);
}
}