generator-begcode
Version:
Spring Boot + Angular/React/Vue in one handy generator
39 lines (38 loc) • 975 B
JavaScript
export class Timer {
_start = 0;
_end = 0;
_add = 0;
start() {
this._start = Date.now();
}
end() {
this._end = Date.now();
}
add(ms) {
this._add += ms;
}
duration() {
const start = this._start;
const end = this._end === 0 && this._start > 0 ? Date.now() : this._end;
return end - start + this._add;
}
getHHMMSS() {
const duration = this.duration();
if (duration <= 0) {
return '00:00:00';
}
const hours = Math.floor(duration / 3600000);
const minutes = Math.floor((duration % 3600000) / 60000);
const seconds = Math.floor((duration % 60000) / 1000);
return `${this._pad(hours)}:${this._pad(minutes)}:${this._pad(seconds)}`;
}
toString() {
return this.getHHMMSS();
}
toJSON() {
return this.toString();
}
_pad(num) {
return num < 10 ? `0${num}` : `${num}`;
}
}