1

I have written a for loop using setInterval. The intention was to "drip feed" the output of the for loop to the user. So basically I would like to send the console.log message every 10000ms and output it on the console of the user.

See my example below:

function longForLoop(limit) {
    for (var i = 0; i < limit; i++) {
        setInterval(() => {
            console.log("This is a long for loop. We are at " + i)
        }, 10000)
    }
}

longForLoop(10)

However, I only get the full result back as a whole?

Any suggestions what I am doing wrong here?

Appreciate your replies!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • 3
    Possible duplicate of [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – melpomene Oct 11 '17 at 04:41
  • 2
    What problem are you really trying to solve? `setInterval()` is non-blocking so your `for` loop immediately runs to completion and meanwhile you've started `limit` timers that will repeat forever. I don't see how this does anything useful and you haven't describe what you're really trying to do. – jfriend00 Oct 11 '17 at 04:45
  • @jfriend00 Thank you for your reply. Please see my updated question. – Carol.Kar Oct 11 '17 at 04:50
  • 1
    So you just want `setInterval(() => { console.log("This is a long for loop. We are at " + i++); }, 10000)` with no `for` loop? – melpomene Oct 11 '17 at 04:51
  • @melpomene I would like to print this message x times. – Carol.Kar Oct 11 '17 at 04:54
  • Then I guess you get to learn how `setInterval` works. – melpomene Oct 11 '17 at 04:55
  • Curious: why are you using `setInterval`, which schedules a never ending stream of calls (unless you explicitly clear it), rather than `setTimeout`? – Mike 'Pomax' Kamermans Oct 11 '17 at 05:10

3 Answers3

3

Don't use setInterval with for loop in these cases as the setInterval will run without condition. use either one of these.I used setInterval without loop and I am using clearInterval once the condition reaches

function longForLoop(limit) {
  var i = 0;
  var ref = setInterval(() => {
    console.log("This is a long for loop. We are at " + ++i);
    if (i == limit) clearInterval(ref);
  }, 1000);
}

longForLoop(10);

In other ways you could also use setTimeout with recursive function

jafarbtech
  • 6,842
  • 1
  • 36
  • 55
0

You can use setTimeout instead of setInterval. Look at my example:

function longForLoop(limit) {
for (var i = 0; i < limit; i++) {
    (function(j) {
       setTimeout(function() {
        console.log('j',j);      
       }, 100 + (1000 * j));
  })(i);
 }
}

longForLoop(10)
kumbhani bhavesh
  • 2,189
  • 1
  • 15
  • 26
-1

The variable i is bound to the value outside your anonymous function. Use code below which passes value of i to function

function longForLoop(limit) {
    for (var i = 0; i < limit; i++) {
        function innerFunction(valOfi)
        {
                console.log("This is a long for loop. We are at " + valOfi);
        }
        setInterval(innerFunction(i) , 10000)
    }
}

longForLoop(10)
SYED SHAH
  • 84
  • 6