Skip to content

Instantly share code, notes, and snippets.

@YAAK
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save YAAK/4cc90c48c2996f52588e to your computer and use it in GitHub Desktop.

Select an option

Save YAAK/4cc90c48c2996f52588e to your computer and use it in GitHub Desktop.
Simple JavaScript Split Lap Timer
var Timer = (function(){
now = Date.now || function () {return (new Date()).getTime();}
var STOP = 0;
var START = 1;
var PAUSE = 2;
function Timer(name, key){
this.name = name || (new Array(10).join().replace(/(.|$)/g, function(){return ((Math.random()*36)|0).toString(36);}));
this.key = key || (new Array(10).join().replace(/(.|$)/g, function(){return ((Math.random()*36)|0).toString(36);}));
_start = 0;
_stop = 0;
_total = 0;
_status = 0; // STOP, START, PAUSE
_laps = [];
}
Timer.prototype.start = function(){
if (_status != START) {
_start = now();
if(_status == STOP)
{
_total = 0;
_laps = [];
}
_status = START;
}
return this;
};
Timer.prototype.stop = function(){
if (_status != STOP) {
_stop = now();
_status = STOP;
_total += _stop - _start;
}
return this;
};
Timer.prototype.pause = function(){
if (_status == START) {
_stop = now();
_status = PAUSE;
_total += _stop - _start;
}
return this;
};
Timer.prototype.resume = function(){
if (_status == PAUSE) {
_status = START;
_start = now();
}
return this;
}
Timer.prototype.lap = function(){
// TODO : Laps
};
Timer.prototype.total = function(){
if (_status == START) {
return (_total + now() - _start);
}
return _total;
};
return Timer;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment