Last active
August 29, 2015 14:18
-
-
Save YAAK/4cc90c48c2996f52588e to your computer and use it in GitHub Desktop.
Revisions
-
YAAK revised this gist
Jul 21, 2015 . No changes.There are no files selected for viewing
-
YAAK revised this gist
Mar 31, 2015 . 1 changed file with 29 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,19 +2,21 @@ var Timer = (function(){ now = Date.now || function () {return (new Date()).getTime();} var txt = function(len){return new Array(len).join().replace(/(.|$)/g, function(){return ((Math.random()*36)|0).toString(36);})} var STOP = 0; var START = 1; var PAUSE = 2; function Timer(name, key){ this.name = name || txt(10); this.key = key || txt(10); _start = 0; _stop = 0; _total = 0; _status = 0; // STOP, START, PAUSE _laps = {}; } Timer.prototype.start = function(){ @@ -23,7 +25,7 @@ var Timer = (function(){ if(_status == STOP) { _total = 0; _laps = {}; } _status = START; } @@ -57,10 +59,30 @@ var Timer = (function(){ return this; } Timer.prototype.lap = function(name, key){ if (_status == START){ var tNow = now(); key = key || txt(10); name = name || txt(10); _laps[key] = { name : name, key : key, saved : tNow, total : _total + tNow - _start }; } return this; }; Timer.prototype.laps = function(key){ if (key){ return _laps[key]; } return _laps; } Timer.prototype.total = function(){ if (_status == START) { return (_total + now() - _start); @@ -70,4 +92,4 @@ var Timer = (function(){ return Timer; })(); -
YAAK created this gist
Mar 31, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ 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; })()