2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
const Timer = function (cb, interval) {
|
2014-10-30 18:25:38 +01:00
|
|
|
this.cb = cb;
|
|
|
|
this.interval = interval;
|
|
|
|
this.timerId = null;
|
|
|
|
};
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
Timer.prototype.start = function () {
|
2014-10-30 18:25:38 +01:00
|
|
|
if (this.timerId != null) {
|
2016-06-18 22:23:19 +02:00
|
|
|
console.warn('timer already started'); // eslint-disable-line no-console
|
2014-10-30 18:25:38 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 22:23:19 +02:00
|
|
|
console.log('starting timer'); // eslint-disable-line no-console
|
2014-10-30 18:25:38 +01:00
|
|
|
this.timerId = setInterval(this.cb, this.interval);
|
2014-11-14 12:20:11 +01:00
|
|
|
this.cb();
|
2014-10-30 18:25:38 +01:00
|
|
|
};
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
Timer.prototype.stop = function () {
|
2014-10-30 18:25:38 +01:00
|
|
|
if (this.timerId == null) {
|
2016-06-18 22:23:19 +02:00
|
|
|
console.warn('no timer running'); // eslint-disable-line no-console
|
2014-10-30 18:25:38 +01:00
|
|
|
} else {
|
2016-06-18 22:23:19 +02:00
|
|
|
console.log('stopping timer'); // eslint-disable-line no-console
|
2014-10-30 18:25:38 +01:00
|
|
|
clearInterval(this.timerId);
|
|
|
|
this.timerId = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-24 22:41:37 +02:00
|
|
|
module.exports = Timer;
|