1

I did see multiple questions related to mine but I couldn't find a proper answer. This is my code but the function inside setInterval is not working. makeAPICall() is not called and the log also didn't execute. Any answer is appreciated.

P.s. I am a newbie so please ignore any mistakes

Code

fetchData() {
        reactotron.log('called!!');
        if (this.props.isConnected) {
            clearInterval(bgTimer);
            bgTimer = setInterval(() => {
                reactotron.log('inside setTimer callback');
                this.makeAPICall();
            }, 20000);
        }
    }

bgTimer is declared above class definition

let bgTimer;

class MyScreen extends Component {
Curious_coder
  • 39
  • 1
  • 7
  • 4
    There's no `=>` function in the code you posted. – Pointy Jul 13 '21 at 15:10
  • What do you mean by "*the function inside setInterval is not working*"? Is it called or not? Does it result in an error? Does it execute successfully but produce the wrong result? – VLAZ Jul 13 '21 at 15:11
  • Define "not working." There's so little context here it'd be difficult to provide any meaningful help. – Dave Newton Jul 13 '21 at 15:11
  • 3
    With that said, *most likely* the problem is that you aren't using an arrow function but you should. See [How to access the correct `this` inside a callback?](https://stackoverflow.com/q/20279484) and [Pass correct “this” context to setTimeout callback?](https://stackoverflow.com/q/2130241) – VLAZ Jul 13 '21 at 15:12
  • 1
    because it will be executed in 20 seconds? – sonenko Jul 13 '21 at 15:57
  • Please log and see what this.props.isConnected have. Probably its failing on this condition and never goes to run the timer – Sameer Kumar Jain Jul 13 '21 at 16:16

0 Answers0