Created
May 2, 2021 00:16
-
-
Save gixxerblade/d6cd07b2ece60b1f624ee607fef992f4 to your computer and use it in GitHub Desktop.
Split Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class SplitTimer { | |
private startTime: number; | |
private lastTime: number; | |
start () { | |
this.startTime = Date.now(); | |
this.lastTime = this.startTime; | |
} | |
split (message: string) { | |
const now = Date.now(); | |
let output = `${message}: -- [Since Last Split: ${SplitTimer.formatTime(now - this.lastTime)}]`; | |
if (this.lastTime !== this.startTime) { | |
output += ` [Total Time Elapsed: ${SplitTimer.formatTime(now - this.startTime)}]`; | |
} | |
console.log(output); | |
this.lastTime = now; | |
} | |
private static formatTime (millis: number): string { | |
if (millis < 300) { return `${millis.toFixed(0)}ms`; } | |
let seconds = millis / 1000; | |
if (seconds < 60) { | |
return seconds.toFixed(1); | |
} | |
const minutes = (seconds / 60).toFixed(0); | |
seconds %= 60; | |
const secondsStr = ((seconds < 10) ? '0' : '') + seconds.toFixed(1); | |
return `${minutes}:${secondsStr}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment