Skip to content

Instantly share code, notes, and snippets.

@joepegler
Created October 15, 2016 09:35
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 joepegler/b401ad943847ac65d23183a6f55fa8a3 to your computer and use it in GitHub Desktop.
Save joepegler/b401ad943847ac65d23183a6f55fa8a3 to your computer and use it in GitHub Desktop.
Simple start/stop Javascript timer
var Timer = (function Timer() {
var vars = {
startTime: 0,
stopTime: 0,
time: 0,
reset: function() {
vars.startTime = 0;
vars.stopTime = 0;
vars.time = 0;
}
}
return function() {
var _this = this;
_this.start = function() {
vars.reset();
vars.startTime = new Date().getTime();
};
_this.stop = function() {
vars.stopTime = new Date().getTime();
vars.time = Math.round((vars.stopTime - vars.startTime) / 10);
return vars.time;
};
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment