Help with Constructor for Pomodoro Clock

Help with Constructor for Pomodoro Clock
0.0 0

#1

Hi crew,

I am trying to create a Timer constructor rather than using global vars. I have issues getting the clear interval to reference to the timer defined in setinterval. This is the code:

$(document).ready(function(){

  // Timer constructor
  function Timer(duration) {
    this.durationTime = duration;
  }

  Timer.prototype.startTimer = function() {
    var timer = setInterval(function(){
      if(this.durationTime >= 0){
       this.durationTime--;
      }
      console.log(this.durationTime);
    },1000);
  }

   Timer.prototype.stopTimer = function() {
     clearInterval(timer);
   };

  var myTimer = new Timer(1000); // 10s timer
  $('#startBtn').click(function(){myTimer.startTimer();})
  $('#stopBtn').click(function(){myTimer.stopTimer();})

}); // end of DRF

#2

var timer scopes the variable to just that function. You could use this.timer.