Skip to content

Instantly share code, notes, and snippets.

@myaumyau
Created February 18, 2013 04:03
Show Gist options
  • Save myaumyau/4975053 to your computer and use it in GitHub Desktop.
Save myaumyau/4975053 to your computer and use it in GitHub Desktop.
[js]Stopwatch
var Stopwatch = function() {
this.initialize.apply(this, arguments);
};
Stopwatch.prototype = {
initialize: function() {
this.reset();
},
start: function() {
this.startTime = new Date();
},
stop: function() {
this.stamp = this.getStamp();
this.startTime = null;
},
reset: function() {
this.stamp = 0;
this.startTime = null;
},
getStamp: function() {
if (this.startTime == null) { return this.stamp; }
return new Date() - this.startTime + this.stamp;
},
getStampString: function() {
var stamp = this.getStamp(), pl = function(v) { return v < 10 ? "0" + v : v; };
var s = (stamp % (60 * 1000)) / 1000;
var m = ((stamp / (60 * 1000)) | 0) % 60;
var h = ((stamp / (60 * 60 * 1000)) | 0) % 60;
return "" + pl(h) + ":" + pl(m) + ":" + pl(s);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment