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.

Revisions

  1. YAAK revised this gist Jul 21, 2015. No changes.
  2. YAAK revised this gist Mar 31, 2015. 1 changed file with 29 additions and 7 deletions.
    36 changes: 29 additions & 7 deletions Timer.js
    Original 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 || (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);}));
    this.name = name || txt(10);
    this.key = key || txt(10);

    _start = 0;
    _stop = 0;
    _total = 0;
    _status = 0; // STOP, START, PAUSE
    _laps = [];
    _laps = {};
    }

    Timer.prototype.start = function(){
    @@ -23,7 +25,7 @@ var Timer = (function(){
    if(_status == STOP)
    {
    _total = 0;
    _laps = [];
    _laps = {};
    }
    _status = START;
    }
    @@ -57,10 +59,30 @@ var Timer = (function(){
    return this;
    }

    Timer.prototype.lap = function(){
    // TODO : Laps
    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;

    })()
    })();
  3. YAAK created this gist Mar 31, 2015.
    73 changes: 73 additions & 0 deletions Timer.js
    Original 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;

    })()