1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-frontend/public/js/utils/Timer.js

29 lines
765 B
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
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);
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;
}
};
module.exports = Timer;