0

With following for loop

for(var i = 0; i<10; i++) {
    setTimeout(function () {
        console.log(i);
    },10);
}

I'm trying to print all values of 'i' inside setTimeout(); With above code i know before reaching to console.log() all 'i's are 10 as setTimeout is async.

So I used below code

for(var i = 0; i<10; i++) {
    setTimeout(console.log(i),10000);
}

I did got output 1-9 but not after 10sec. How can i get values of 'i' from 1-9 with out storing in a different variable outside setTimeout. Is it possible to do so?

NAVIN
  • 3,193
  • 4
  • 19
  • 32
  • `function req(i) { if (i >= 0) { setTimeout(function () { console.log(i); req(--i); }, 1000); } }` – Oleg Pnk May 22 '17 at 13:41
  • this value of "i" had to come via for loop only and setTimeout had to be inside that for loop. Is this possible like that or not. – NAVIN May 23 '17 at 06:16

0 Answers0