Switch to easytimer lib (so we can pause!)

This commit is contained in:
Ross
2020-04-30 23:04:22 +01:00
parent 38138600fc
commit e75294f97e
5 changed files with 65 additions and 75 deletions
+1 -53
View File
@@ -39,56 +39,4 @@ export function formatBytes(bytes, decimals = 2) {
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
export function CountDownTimer(duration, granularity) {
this.duration = duration;
this.granularity = granularity || 1000;
this.tickFtns = [];
this.running = false;
}
CountDownTimer.prototype.start = function () {
if (this.running) {
return;
}
this.running = true;
var start = Date.now(),
that = this,
diff,
obj;
(function timer() {
diff = that.duration - (((Date.now() - start) / 1000) | 0);
if (diff > 0) {
setTimeout(timer, that.granularity);
} else {
diff = 0;
that.running = false;
}
obj = CountDownTimer.parse(diff);
that.tickFtns.forEach(function (ftn) {
ftn.call(this, obj.minutes, obj.seconds);
}, that);
})();
};
CountDownTimer.prototype.onTick = function (ftn) {
if (typeof ftn === 'function') {
this.tickFtns.push(ftn);
}
return this;
};
CountDownTimer.prototype.expired = function () {
return !this.running;
};
CountDownTimer.parse = function (seconds) {
return {
'minutes': (seconds / 60) | 0,
seconds: seconds % 60 | 0,
};
};
}