0

I'd like to use this to referring to the class properties while instead is referring to the function itself. How can I do?

class Person {

    isWalking = false
    steps = 10

    walk() {
        this.isWalking = setInterval(function () {
            if (this.steps <= 1) {
                clearInterval(this.isWalking)
                this.isWalking = false
            }
            --this.steps
            console.log(this.steps)
        }, 1000)
    }

}
KaMZaTa
  • 535
  • 2
  • 9
  • 24

1 Answers1

1

Covert your setInterval callback to an arrow function

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36