3

I would like to know if there is a way to use setTimeout recursive implements with an arrow function, in order to use this (refers to my class attribute for example) inside. Indeed, this = undefined when i declare my setTimeout with a normal function

I got :

public currentIndex: number = 0;

setTimeout(function run(){
    this.currentIndex++;
    console.log(this.currentIndex); // returns undefined
    setTimeout(run, 1000);
}, 1000)

Instead of :

setTimeout(() => {
    this.currentIndex++;
    console.log(this.currentIndex) // returns currentIndex value
    setTimeout( ?? , 1000) // What should i put instead of '??' ?
}, 1000)
Fab83i
  • 85
  • 7
  • Are you looking for `setInterval` instead to do your recursive job? https://developer.mozilla.org/en-US/docs/Web/API/setInterval – Himanshu Aggarwal Nov 10 '21 at 09:17
  • As I answered to ciekals11, `setInterval` seems to not be adapted in my case, due to executing time of my code inside – Fab83i Nov 10 '21 at 09:21
  • Another approach could be a loop that pauses between iterations using `await` like `for(;;await delay(1000)){ this.currentIndex++; }` – Thomas Nov 10 '21 at 10:21

4 Answers4

1

You could bind this first and then use this function for all calls.

function run(reference) {
    this.currentIndex++;
    console.log(this.currentIndex); // returns undefined
    setTimeout(reference, 1000, reference);
}

const runThis = run.bind(thisReference);

setTimeout(runThis, 1000, runThis);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Its because arrow function does not create new context inside arrow function body but normal function does. So this in arrow function refers to parent scope context but this in normal function refers to its own context.

1

This will create setTimeouts recursively

let currentIndex = 0;

const run = () => {
    setTimeout(() => {
        currentIndex++;
        console.log(currentIndex);
        run();
    }, 1000);
}

run();

but better approach may be (I don't know your use case, so it is just maybe) to use setInterval()

let currentIndex = 0;

const interval = setInterval(() => {
    currentIndex++;
    console.log(currentIndex);

    // stop interval
    if (currentIndex >= 10) {
        clearInterval(interval);
    }
}, 1000);
ciekals11
  • 2,032
  • 1
  • 10
  • 24
  • I would have used `setInterval` but I saw that the interval chosen is not guarantee, due to execution time of the program. For example, if the code inside take longer than expected, the time will be impacted. Is that true ? – Fab83i Nov 10 '21 at 09:19
  • I think that [this](https://stackoverflow.com/a/1976535/9074272) answers your question – ciekals11 Nov 10 '21 at 09:21
0

Probably the easiest way is to extract the arrow function into its own variable:

const run = () => {
    this.currentIndex++;
    console.log(this.currentIndex);
    setTimeout(run, 1000);
};
setTimeout(run, 1000);

Though in this particular example you could simplify it even more using setInterval instead of setTimeout, avoiding the second setTimeout call entirely.

p4m
  • 356
  • 3
  • 9