I can't seem to figure out why using the function name in setInterval is causing issues, while passing an anonymous function works perfectly fine.
In the example that's not working (it's logging NaN to the console and before the first call, this.counter++ returns undefined as it couldn't find the variable?)
export class MyClassName {
counter = 0;
startInterval(){
setInterval(this.myFunc , 1000)
}
myFunc(){
this.counter++;
console.log(this.counter)
}
}
However, when changing startInterval like below, it works correctly
startInterval(){
setInterval(() => this.myFunc() , 1000)
}
And in the HTML we have:
<button (click)="startInterval()">Start</button>