Created
February 18, 2013 04:03
-
-
Save myaumyau/4975053 to your computer and use it in GitHub Desktop.
[js]Stopwatch
This file contains hidden or 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
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