5

Here, i tried to set timeout for every iteration but i could not make it because nature of nodejs. Is there any way to do that?

Thanks in advance

for (var i = 1; i <= 1000; i++) {
    setTimeout(function () { console.log('something') }, 3000); 
}
Serhat Ates
  • 885
  • 1
  • 12
  • 18

3 Answers3

7

It works but it schedules all timeouts at the same time.

If you want to schedule them at 3 sec intervals then use:

for (var i = 1; i <= 1000; i++) {
    setTimeout(function () { console.log('something') }, i * 3000);
}

If you want to use i inside of your timeout callbacks, use let instead of var like this:

for (let i = 1; i <= 1000; i++) {
    setTimeout(function () { console.log('something', i) }, i * 3000);
}

As you can see with var it would print 1001 for every line:

for (var i = 1; i <= 1000; i++) {
    setTimeout(function () { console.log('something', i) }, i * 3000);
}

And by the way, you can simplify it with arrow function syntax:

for (let i = 1; i <= 1000; i++) {
    setTimeout(() => console.log('something', i), i * 3000);
}

Another way to do it would be to do something like this - instead of scheduling all 1000 timeouts at the same time, create an interval:

(() => {
    let i = 0;
    setInterval(() => {
        i++;
        console.log('something', i);
    }, 3000);
})();

The outer closure is to keep the i variable from being visible in the outer scope. OR you can use something like this:

(() => {
    let i = 0;
    let f = () => {
        i++;
        console.log('something', i);
        setTimeout(f, 3000);
    };
    setTimeout(f, 3000);
})();

In the last example the function that is invoked as a timeout callback schedules itself every time it finishes.

There are many ways to do it and all have some pros and cons.

For example you shouldn't use setInterval if your callback could potentially run longer than the interval between invocations. It will not be a problem when you use setTimeout and schedule yourself in every callback but on the other hand you may have less precision in the intervals that way. You need to test what works best for you.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • What i see is , loop scheduled all setTimeout at 3 seconds delay, but every iteration there is some time pass like 100ns..But we can not see these delay time in every in between iterations it s about processor power. Am i right? Loops iterates sync manner but when it is done then go back async manner? – Serhat Ates Mar 15 '17 at 14:52
2

for (var i = 1; i <= 10; i++) {
     wait('something', i, 10);
}

function wait(msg, i, length){
    setTimeout(function () 
    { 
    console.log(msg) ;
    }, (i * 3000));
}

try something like this. Your code actually works but you wait 3 seconds for all which will wait about 1 ms before the next iteration is done runs

tyehia
  • 257
  • 5
  • 16
0

I think what you're really looking for is setInterval.

let runMeEveryThreeSeconds = function() {
  console.log('hello!')
}
let threeSecondTimer = setInterval(runMeEveryThreeSeconds, 3000)

The timer will run forever, and every three seconds the word 'hello!' will be printed to your console. When you are ready to cancel the timer, you will need to:

clearInterval(threeSecondTimer)
Dom Vinyard
  • 2,038
  • 1
  • 23
  • 36
  • Thank you for your time but this is not solution, i was actually looking for usage of timers in Node.js loop. This code snippet not works in for loop. – Serhat Ates Mar 15 '17 at 14:16