UNPKG

@awayjs/core

Version:
86 lines (85 loc) 2.71 kB
import { __extends } from "tslib"; import { ErrorBase } from '../errors/ErrorBase'; import { EventDispatcher } from '../events/EventDispatcher'; import { TimerEvent } from '../events/TimerEvent'; var Timer = /** @class */ (function (_super) { __extends(Timer, _super); function Timer(delay, repeatCount) { if (repeatCount === void 0) { repeatCount = 0; } var _this = _super.call(this) || this; _this._repeatCount = 0; _this._currentCount = 0; _this._running = false; _this._delay = delay; _this._repeatCount = repeatCount; if (isNaN(delay) || delay < 0) throw new ErrorBase('Delay is negative or not a number'); return _this; } Object.defineProperty(Timer.prototype, "currentCount", { get: function () { return this._currentCount; }, enumerable: false, configurable: true }); Object.defineProperty(Timer.prototype, "delay", { get: function () { return this._delay; }, set: function (value) { this._delay = value; if (this._running) { this.stop(); this.start(); } }, enumerable: false, configurable: true }); Object.defineProperty(Timer.prototype, "repeatCount", { get: function () { return this._repeatCount; }, set: function (value) { this._repeatCount = value; }, enumerable: false, configurable: true }); Timer.prototype.reset = function () { if (this._running) this.stop(); this._currentCount = 0; }; Object.defineProperty(Timer.prototype, "running", { get: function () { return this._running; }, enumerable: false, configurable: true }); Timer.prototype.start = function () { var _this = this; this._running = true; clearInterval(this._iid); this._iid = setInterval(function () { return _this.tick(); }, this._delay); }; Timer.prototype.stop = function () { this._running = false; clearInterval(this._iid); }; Timer.prototype.tick = function () { this._currentCount++; if ((this._repeatCount > 0) && this._currentCount >= this._repeatCount) { this.stop(); this.dispatchEvent(new TimerEvent(TimerEvent.TIMER)); this.dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE)); } else { this.dispatchEvent(new TimerEvent(TimerEvent.TIMER)); } }; return Timer; }(EventDispatcher)); export { Timer };