Skip to content

Instantly share code, notes, and snippets.

@Jeff-Russ
Created May 15, 2016 04:40
Show Gist options
  • Save Jeff-Russ/50a3048c6e0b53c68970bafa7ed4e622 to your computer and use it in GitHub Desktop.
Save Jeff-Russ/50a3048c6e0b53c68970bafa7ed4e622 to your computer and use it in GitHub Desktop.
Construct a JS obj to determine time elapsed in ms
// TimeElapsed constructs an object for use in determining
// time elapsed in milliseconds
// constructor:
function TimeElapsed(){
var start, elapsed;
function getstamp(){ return (new Date().getTime());}
this.get = function(){
var now = getstamp();
elapsed = now - start;
return elapsed;
};
this.reset = function(){
start = getstamp();
elapsed = 0;
return 0;
};
this.reset();
}
/* Here it is in use with a very fast typist!
var t1 = new TimeElapsed(); // make new timer
// .get() returns ms since creation of object
t1.get() // returns 810
t1.get() // returns 2020
var t2 = new TimeElapsed(); // make another timer
t2.get() // returns 803
t1.get() // returns 12552
// reset counter for object:
t1.reset() // returns 0
t1.get() // returns 810
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment