skip to Main Content

I have a for loop that i need to use to iterate through 6 arrays of equal length at the same time but at set intervals. I’m using Javascript. what i am having trouble with is implementing the intervals. Basically what i am trying to do is make the for loop iterate through each array at an interval of 1000ms, so that it prints out the console logs at a rate of 1000ms. The ‘iteration complete’ log only after the array is done. meaning after every index has been iterated through at a rate of 1000 ms per index

if anyone has any advice please i would greatly appreciate your input.

const noteW = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
const noteE = [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
const noteR = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
const noteI = [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
const noteO = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
const noteP = [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]

for (i=0;i<noteW.length;i++){
  

      if (noteW[i] === 1) {
        console.log('shieeet1');
      }
      if (noteE[i] === 1) {
        console.log('shieeet2');
      }
      if (noteR[i] === 1) {
        console.log('shieeet3');
      }
      if (noteI[i] === 1) {
        console.log('shieeet4');
      }
      if (noteO[i] === 1) {
        console.log('shieeet5');
      }
      if (noteP[i] === 1) {
        console.log('shieeet6');
      }
      console.log('Iteration complete');
    } 

I have tried using setintervals but its not working for some reason.

2

Answers


  1. You can achieve your purpose by using a setTimeout with Promise which resolves after 1 second.

    A shorter version of the working code will look like:

    const notes = [
      [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],
      [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],
      [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],
      [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],
    ];
    
    async function printLogs() {
      for (let i = 0; i < notes[0].length; i++) {
        for (let j = 0; j < notes.length; j++) {
          if (notes[j][i] === 1) {
            console.log(`shieeet${j + 1}`);
          }
        }
        console.log(`${i + 1}th iteration complete`);
    
        // Wait for 1000ms before continuing the loop
        await new Promise((resolve) => setTimeout(resolve, 1000));
      }
      console.log("Loop completes"); // <--- From your comment put your Iteration complete here if you want.
      
    }
    
    printLogs();
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    Alternative solution:

    You might be using setInterval wrongly because of which it was not working for you. Here’s how you can use it:

    const notes = [
      [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
      [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
      [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
      [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
    ];
    let i = 0;
    function printLogs() {
      if (i >= notes[0].length) {
        clearInterval(interval);
        console.log(`Iteration complete`);
        return;
      }
      for (let j = 0; j < notes.length; j++) {
        if (notes[j][i] === 1) {
          console.log(`shieeet${j + 1}`);
        }
      }
      i++;
    }
    const interval = setInterval(printLogs, 1000);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. From what I understand from your question, you want to have only one message each second.

    IMHO, the simplest is to use a generative function.

    const
      delay = 1000  // ms
    , noteW = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
    , noteE = [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
    , noteR = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
    , noteI = [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
    , noteO = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
    , noteP = [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1]
    , Notes =
      [ [noteW, 'shieeet1'], [noteE, 'shieeet2'], [noteR, 'shieeet3']
      , [noteI, 'shieeet4'], [noteO, 'shieeet5'], [noteP, 'shieeet6']
      ]
    , iterator = 
        (function* ()
          {
          for (let i=0; i < noteW.length; i++ )
            {
            for (let [note,resp] of Notes)
              {
              if (note[i]) yield resp;
          } } }
        )()
    , refInterval = 
        setInterval( () => 
          {
          let iteration = iterator.next();
          //console.clear();
          if (iteration.done)
            {
            console.log( 'Iteration complete' );
            clearInterval( refInterval );
            }
          else
            console.log( iteration.value );
          }
          , delay
        );
    .as-console-wrapper    { max-height: 100% !important;top: 0; }
    .as-console-row::after { display: none !important;           }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search