0

I want to call the function callMe() every 1000 ms. This works so far, but I cannot call another function out of callMe(). I get the following Error:

uncaught TypeError: this.sortTargets is not a function

My class variable:

private intervalId : any;

The code/functions

constructor () {
    this.intervalId = setInterval(this.checkLabelConflicts, 1000);
  }

  private sortTargets() {
    console.log("hello world");
  }

  private checkFunction()  {
  this.sortTargets();
}

Why am I not able to call other functions out of the intervaled function? I already tried to set the constructor to bottom of the code, it feels like this is a declaration missing error. If someone has another solution without setInterval (e.g. workers/threads?) you're welcome to explain it to me.

Happy monday

IAfanasov
  • 4,775
  • 3
  • 27
  • 42
Nils T
  • 9
  • 2

1 Answers1

-1

How to access the correct `this` inside a callback

constructor () {
    this.intervalId = setInterval(this.checkLabelConflicts.bind(this), 1000);
  }

  private sortTargets() {
    console.log("hello world");
  }

  private checkFunction()  {
  this.sortTargets();
}

I had to .bind(this)

Nils T
  • 9
  • 2