+ 1

Whenever I use setTimeout in sololearn nodejs, it says terminated. Why please?

https://code.sololearn.com/cOKAy7B2q6QO/?ref=app

1st Feb 2021, 6:42 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
5 odpowiedzi
+ 1
more over write console.log in for loop before setTimeout, and you see that when setTimeout called your loop finished. it is idea of async functions.
1st Feb 2021, 6:52 PM
george
george - avatar
+ 1
george I didn't get you clearly. I wanted to display the numbers after every second. From 0 to 5.
1st Feb 2021, 6:57 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 1
Please use setInterval function.
1st Feb 2021, 7:25 PM
george
george - avatar
+ 1
let index=-1; const id=setInterval(f,1000); function f(){ console.log(++index); if(index==5){ clearInterval(id) } }
1st Feb 2021, 7:32 PM
george
george - avatar
+ 1
george thanks a million 😊😊😊😊
1st Feb 2021, 7:58 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar