1

I'm having this error when I call a method from another method in a class. This method is been called from setInterval.

class App {
    b() {
        console.log("BBBBBBBB")
    }

    t() {
        console.log("TTTTTTT")
        this.b();
    }
}


const t = new App();


setInterval(t.t, 1000);
Gabriel costa
  • 151
  • 2
  • 14

1 Answers1

3

You need to bind the method to the variable so the value of this stays constant. Read this page for more information.

setInterval(t.t.bind(t), 1000);
Aplet123
  • 33,825
  • 1
  • 29
  • 55