through - settimeout javascript




How to Change Interval Time Dynamically in For Loop According to Index/iteration Number? (5)

Since I could not comment, I am forced to write this post. I got the below code which delays/waits exactly 1 seconds or 1000 milliseconds -

let n = 5;
for (let i=1; i<n; i++)
{
  setTimeout( function timer()
  {
      console.log("hello world");
  }, i*1000 );
}

But how can I delay it i*1000 seconds instead of fixed 1000 milliseconds so the waiting depends on iteration number ?

For example, if n= 5 , then I want the loop delay 1 second in 1st iteration. 2 seconds in second iteration, and so on.. the final delay will 5 seconds.


Here is a function that will show immediately, then 1 second later, 2 seconds after than, 3 seconds after that etc. No special math, no promises needed

const n = 5;
let cnt=0;

function show() {
  console.log("call "+cnt,"delay: ",cnt,"sec");
  cnt++;
  if (cnt > n) return; // we are done
  setTimeout(show, cnt*1000 ); // cnt seconds later
}
show()


Loop doesn't waits for timeout function to be completed. So, when the loop runs it schedules your alert for each index.

You can use a function which will run according to your index but scheduled at same time. You can feel the difference of 3 seconds.

function test(i){
    setTimeout( function timer(){
        console.log("hello world" + i);
    }, i*3000);
}
for (let i=1; i<4; i++) {
   test(i);
}

Use recursive calls instead of for loop

let i=1;
function a(i) {
  if (i > 5)
    return
  else
    b("message", i)
}

function b(s, f) {
  setTimeout(function timer() {
    console.log(s + " " + f + " seconds");
  }, f * 1000);
  a(++i);
}
a(i);


While this task could be solved with promises, reactive streams and other cool tools (hey, nobody has suggested using workers yet!), it can also be solved with a little arithmetics.

So you want timeouts in a sequence: 1s, the previous one + 2s, the previous one + 3s, and so on. This sequence is: 1, 3, 6, 10, 15... and its formula is a[n] = n * (n + 1) / 2 . Knowing that...

let n = 6;
console.log(new Date().getSeconds());

for (let i = 1; i < n; i++) {
  setTimeout(function timer() {
    console.log(new Date().getSeconds());
  }, 1000 * i * (i + 1) / 2);
}


ON EXPLORING @mbojko 's Answer

for (let i = 1; i <= 5; i++) {
  setTimeout(function timer() {
    console.log("Delaying - " + i + " seconds");
  }, 1000 * i * (i + 1) / 2);
}

function Explanation(){
  //This comes from the finite arithmetic series n(n+1)/2
            //http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/runsums/triNbProof.html
            //for first five iterations..
            //1000 * 1 * (1+1)/2 = 1
            //1000 * 2 * (2+1)/2 = 3
            //1000 * 3 * (3+1)/2 = 6
            //1000 * 4 * (4+1)/2 = 10
            //1000 * 5 * (5+1)/2 = 15
}





wait