Skip to content

Instantly share code, notes, and snippets.

@gixxerblade
Created May 2, 2021 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gixxerblade/d6cd07b2ece60b1f624ee607fef992f4 to your computer and use it in GitHub Desktop.
Save gixxerblade/d6cd07b2ece60b1f624ee607fef992f4 to your computer and use it in GitHub Desktop.
Split Timer
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