Having an issue with my countdown timer function:
startTimer(duration) {
this.myTimer = duration;
setInterval(function () {
this.myTimer--;
console.log("TIMER: " + typeof(this.myTimer) + " " + this.myTimer);
}, 1000);
}
When I call it using this.startTimer(60);
,
The console.log
output shows: TIMER: number NaN
. It seems that typeof(this.myTimer)
is returning number
, but the value is NaN
.
Any idea why it's returning NaN
?
Just to clarify, I'm using Angular2 (typescript) and I've declared the variable myTimer
within the component class before the constructor, as shown below:
export class myComponent {
myTimer;
constructor(private navCtrl: NavController) {}
// other code goes here
}