Here is the code I am currently using to increment the value of intVariable using window.setInterval.
var Arrow = (function () {
function Arrow() {
this.intVariable = 1;
this.itemId = -1;
this.interval = 25;
}
Arrow.prototype.activateTimer = function () {
if (this.itemId === -1) {
window.setInterval(this.showTimer(), this.interval);
}
};
Arrow.prototype.showTimer = function () {
this.intVariable += this.interval;
console.log(this.intVariable);
};
return Arrow;
}());
var arrow = new Arrow();
arrow.activateTimer();
When I use the line below, the showTimer function is only called once:
window.setInterval(this.showTimer(), this.interval);
However, when I change it to:
window.setInterval(() => this.showTimer(), this.interval);
It works flawlessly.
I'm curious why it worked using an arrow function and would appreciate some insight.