Stack Overflow : Stop and run again timer via pure javascript

http://stackoverflow.com/questions/35533747/stop-and-run-again-timer-via-pure-javascript

by Lucy Derlin

HTML

<div class='session'></div>
<div id='increase' onclick='decrease()'>-</div>
<div id='increase' onclick='increase()'>+</div>
<div id='timer' onclick='general()'></div>

JavaScript

window.onload = init;

    var minutes, x, timer;

    function init() {
      x = document.getElementsByClassName('session');
      timer = new MyTimer(document.getElementById("timer"));
      minutes = 0;
    }


    function MyTimer(htmlEl) {
      this.sec = 0;
      this.min = 0;
      this.elt = htmlEl;
    }

    MyTimer.prototype.set = function(m) {
      this.min = m;
      this.display();
      var self = this;
      this._dec = function() {
        self.sec--;
        if (self.sec < 0) {
          if (self.min == 0) {
            self.stop();
          } else {
            self.min -= 1;
            self.sec = 59;
          }
        }
        self.display();
      }
    }

    MyTimer.prototype.display = function() {
      this.elt.innerHTML = this.min + ":" + this.sec;
    }



    MyTimer.prototype.toggle = function() {
      if (this.interval) {
        this.stop();
        this.interval = undefined;
      } else this.start();
    }

    MyTimer.prototype.start = function() {
      this.interval = setInterval(this._dec, 100);
    };

    MyTimer.prototype.stop = function() {
      clearInterval(this.interval);
    };


    function increase() {
      minutes += 1;
      x[0].innerHTML = minutes;
      timer.set(minutes);
    }

    function decrease() {
      minutes -= 1;
      x[0].innerHTML = minutes;
      timer.set(minutes);
    }


    function general() {
      timer.toggle();
    }