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