user3646717 user3646717 - 23 days ago 8
TypeScript Question

"this" not being recognized inside a function

I have a property defined but when I try to access it from inside a setInterval anonymous function, it is not being recognized. = 6;
useTimer() {
let timer = setInterval(
function () {;//here the keyword this is not being recognized
}, 1000

Answer Source

The problem occurs because you are not not using an Arrow Function.

Arrow functions take this from their outer execution context.

The ref mentions:

arrow functions which do provide their own this binding (it remains the this value of the enclosing lexical context).

So just to this instead:

let timer = setInterval(
  () => {; // 'this' takes its value from the outer context
  }, 1000