Skip to content

Instantly share code, notes, and snippets.

@victornpb
Created April 23, 2014 04:24
Show Gist options
  • Save victornpb/11202690 to your computer and use it in GitHub Desktop.
Save victornpb/11202690 to your computer and use it in GitHub Desktop.
Stopwatch constructor to measure the elapsed time
/** Stopwatch constructor to measure the elapsed time
* @author Victor B - www.vitim.us
*/
function Stopwatch(){
this.running = false;
this.startTimestamp;
this.endTimestamp;
}
Stopwatch.prototype.start = function(){
this.running = true;
return this.startTimestamp = this.endTimestamp = Date.now();
}
Stopwatch.prototype.stop = function(){
this.running = false;
this.endTimestamp = Date.now();
return this.endTimestamp;
}
Stopwatch.prototype.elapsed = function(){
if(this.running) this.endTimestamp = Date.now();
return this.endTimestamp - this.startTimestamp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment