0

I get an error when I write this recursive function in Angular 2 (Typescript):

ngOnInit() {
    this.countdown(6)
}

countdown(time) {
    this.timeLeft = time - 1
    console.log(this.timeLeft)
    setInterval(function() {
      this.countdown(this.timeLeft)
    }, 1000)
 }

timeLeft is declared as a class property.

Here is the error I get:

EXCEPTION: this.countdown is not a function

What am I doing wrong?

Thanks!

Kim Kern
  • 54,283
  • 17
  • 197
  • 195
slotdp02
  • 438
  • 1
  • 6
  • 15
  • http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work/3127440#3127440 – Dan O Mar 22 '17 at 23:28
  • You definitely do not want to start a new *interval* on every recursive call. Use `setTimeout` instead. And don't forget the `if (this.timeLeft > 0)` condition. – Bergi Mar 22 '17 at 23:35

0 Answers0