-1

In my code i want to loop again once a certain condition if(i === arr.length) matched. here is the demo code. any idea how can i solve it?

var arr = [1,2,3];

for (let i = 0; i < arr.length; i++)
{
  setTimeout( function timer()
  {
      console.log("test");
      if(i === arr.length){
      // start the looping after this condition match
      }
  }, i*3000 );
}

6
  • 1
    I am not sure I fully understand, but you can set i=0 inside your if-statement to restart the loop Commented Aug 6, 2020 at 11:32
  • Your loop is finished before even the first test is written to the console. What are you actually trying to achieve. Essentially you've presented us an XY Problem Commented Aug 6, 2020 at 11:35
  • @RIYAL I think you should explain what you're trying to do, rather than fix what you're trying to do. I bet there's a much easier approach to you problem :) Commented Aug 6, 2020 at 11:38
  • Put the loop in a function and call that function whenever you want the loop to run Commented Aug 6, 2020 at 11:40
  • Also your if statement is never true since i will have values 0 1 2 and arr.length is 3 Commented Aug 6, 2020 at 11:41

3 Answers 3

1

Your code never reaches the condition since i < arr.length is your exit, if you change it to i <= arr.length then the condition will be reached,

then to re-enter the loop i suggest that you encapsulate all your loop in a function

var arr = [1,2,3];

for (let i = 0; i < arr.length; i++)
{
  setTimeout( function timer()
  {
      console.log("test");
      if(i === arr.length){
      // start the looping after this condition match
      }
  }, i*3000 );
}

To

var arr = [1,2,3];

function Looper(){
   for (let i = 0; i <= arr.length; i++)
   {
     setTimeout( function timer()
     {
         console.log("test");
         if(i === arr.length){
             Looper();
         }
     }, i*3000 );
   }
}

Looper();

Keep in mind that this is an infinite loop, you would better use setInterval, if thats your goal, but if you have something else in mind hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem with your code as it is now is the loop is exited before even the first setTimeout call back runs. The addition of a console.log after the loop confirms this.

var arr = [1,2,3];

for (let i = 0; i < arr.length; i++)
{
  setTimeout( function timer()
  {
      console.log("test");
      if(i === arr.length){
      // start the looping after this condition match
      }
  }, i*3000 );
}
console.log("Loop exited");

So there is no way to restart that loop. However you can re-organise your code to keep looping over your arr with a 3000ms delay. The easiest is probably with a Promise.

var arr = [1,2,3];

async function doWork(item){
    return new Promise(resolve => {
        setTimeout(() => {
            console.log("finished",item); // do whatever you want to do here
            resolve();
        },3000);
    });
}


async function test(){
  var exit = false; // if you set this to true elsewhere the code will stop
  var i = 0;

  while(!exit){
    var item = arr[i++ % arr.length];
    await doWork(item);
  }
}

test();

Comments

0

Here is simple attempt hope you'll find it helpful

var arr = [1, 2, 3];
var i = 0;

var i = 0;

function timer() {
  console.log(arr[i])
  i = (i + 1) % arr.length;
}
setInterval(timer, 1000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.