question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scope of this is lost when passing a member function to setInterval

See original GitHub issue

TypeScript Version: 1.8.10

When passing a member function to setInterval, the scope of ‘this’ is lost within the callback, though the structure of the code (given experience of any object orientated language) indicates it shouldn’t be.

Example code

export class SetIntervalTest {
  private someNumber: number = 1;

  trigger() {
      setInterval(this.setIntervalCallback, 400);
  }

  private setIntervalCallback() {
      console.log(this.someNumber);
  }
}  

Expected behavior When console.log(this.someNumber); is called, the scope of this is within the scope of the SetIntervalTest interval instance, printing ‘1’.

Actual behavior: The scope of this is pulled from global scope, resulting in this.someNumber being ‘undefined’. If that isn’t possible, the compiler should indicate the expected behaviour is not what you’re going to get.

To fix this i need to change ‘setInterval(this.setIntervalCallback, 400);’ to ‘setInterval(() => this.setIntervalCallback(), 400);’ so ‘this’ is correctly scoped in the callback.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

11reactions
RyanCavanaughcommented, Aug 11, 2016

See “something about this” in https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

You can have the “correct” code generated by using an arrow function:

    private setIntervalCallback = () => {
      console.log(this.someNumber);
    };
3reactions
zijianhuangcommented, Sep 9, 2019

@leewinder ,

setInterval(()=>this.setIntervalCallback(), 400);

should be just working fine so that the callback can see this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setInterval with passed function does not recognize global ...
I wanted to test setInterval() function and counter some variable, not something too hard, and I got stuck in problem that I couldn't...
Read more >
How to Fix JavaScript Callbacks Variable Scope Problems
The callbackClosure function returns a function that invokes the actual callback with an explicit copy of i as an argument. 1var array =...
Read more >
Function binding
Once a method is passed somewhere separately from the object – this is lost. Here's how it may happen with setTimeout :.
Read more >
How to pass parameter in JavaScript function, Javascript pass object ...
TypeScript Version: 1.8.10. When passing a member function to setInterval, the scope of 'this' is lost within the callback, though the structure of...
Read more >
What to do when “this” loses context
A way to fix the problem is to create new functions in the constructor using bind(this) . constructor(){ super(); this.todos = []; this....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found