I don't understand why using function name in setInterval is not working correctly but passing annonymous function is working just right.
not working example (it's console logging NaN and before calling first time this.counter++ it's returning 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)
}
}
but with startInterval changed like below it's working correctly
startInterval(){
setInterval(() => this.myFunc() , 1000)
}
and in html we have
<button (click)="startInterval()">Start</button>