I am having trouble with making a counter Object. What I want it to do is to start a timer with a newly created object. The timer does not start unless the object variables are set as global variables. Can someone take a look at this and help me out?
Here is the code:
function Clock(timeInMinutes){
this.seconds = timeInMinutes * 60;
this.current = 0;
}
Clock.prototype = {
constructor: Clock,
start: function(){
function increase(){
if(this.current <= this.seconds){
console.log(this.current);
this.current++;
}
}
setInterval(increase,1000);
}
};
When I enter this code nothing happens:
var clock = new Clock(1);
clock.start();
Things only happen when i do this:
this.current = 0;
this.seconds = 60;
Why is the 'this' value inside the start function not working as I want it do be?