From what I have gathered from other posts it looks like it is not possible, but why can't i call a setInterval function (slightly shorter hand) like this?
tick: function() {
var self = this;
setInterval(this.calculateTime, 1000);
}
I need to use the following.
tick: function() {
var self = this;
setInterval(function(){this.calculateTime()}, 1000);
}
Can anyone explain why this is the case and put my mind at ease?
Please see complete code below
var countdownTimer = {
init: function(end) {
this.endTime = new Date(end);
},
calculateTime: function(){
this.now = new Date();
this.difference = this.endTime - this.now;
this.seconds = Math.floor(this.difference / 1000);
this.minutes = Math.floor(this.seconds / 60);
this.hours = Math.floor(this.minutes / 60);
this.days = Math.floor(this.hours / 24);
this.hours %= 24;
this.minutes %= 60;
this.seconds %= 60;
},
tick: function() {
var self = this;
setInterval(function(){this.calculateTime()}, 1000);
}
}
countdownTimer.init('02/09/2016');
countdownTimer.calculateTime();
countdownTimer.tick();