export class CallbackThrottle { private lastCallbackExecuted: number; private throttleTime: number; constructor(ms: number) { this.throttleTime = ms; this.lastCallbackExecuted = 0; } setThrottleTimer(ms: number) { this.throttleTime = ms; } queueCallback(func: Function) { if (!func) { throw new Error('CallbackThrottle.queueCallback(). No callback.'); } this._doCallback(func); } _doCallback(func: Function) { let rightNow = Date.now(); if (rightNow >= this.lastCallbackExecuted + this.throttleTime) { this.lastCallbackExecuted = rightNow; func(); } else { setTimeout(() => { this._doCallback(func); }, this.lastCallbackExecuted + this.throttleTime); } } }