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)
setIntervalinstead to do your recursive job? developer.mozilla.org/en-US/docs/Web/API/setIntervalsetIntervalseems to not be adapted in my case, due to executing time of my code insideawaitlikefor(;;await delay(1000)){ this.currentIndex++; }