# For loop with a delay?

**URL:** <https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167>\
**Category:** General\
**Created:** [February 28, 2018, 1:27pm UTC](https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167 "2018-02-28T13:27:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wynand](https://avatars.discourse-cdn.com/v4/letter/w/e68b1a/32.png) [@Wynand](https://forum.freecodecamp.org/u/Wynand)\
**Post date:** [February 28, 2018, 1:27pm UTC](https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167/1 "2018-02-28T13:27:31Z")

</div>

Hi everybody

I am busy with the Simon game and having some problems.  
I have an array that holds the sequence for the computer. Now I am trying to iterate through the computer array at a pace of one index every 1.5 second and at every index I want the corresponding sound to play. With the code I am using (below) all the sounds associated with every index plays at the same time.

```
  function sequence(computerArr) {
  if (computerArr.length === count) {
  
  var interval = setInterval(function(){                        
   for (var i = 0; i < computerArr.length; i++) {
        if (computerArr[i] === 0) {
            soundRed.load();
            soundRed.play();
            clearInterval(interval);

        } else if (computerArr[i] === 1) {
             soundBlue.load();  
             soundBlue.play();
             clearInterval(interval);

        } else if (computerArr[i] === 2) {
             soundGreen.load();
             soundGreen.play();
             clearInterval(interval);

        } else if (computerArr[i] === 3) {
             soundYellow.load();
             soundYellow.play();
             clearInterval(interval);
        } 
       }
      }, 1500);         
    }             
 };

```

I am guessing the way I have it, is that every 1.5 seconds the whole for loop is executed, and that is why all the sounds of all the indices play together.  
How can I iterate through the array one index at a time every 1.5 seconds?

---

<div class="post-metadata">

**Author:** ![eduardo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/eduardo/32/80674_2.png) [@eduardo](https://forum.freecodecamp.org/u/eduardo)\
**Post date:** [February 28, 2018, 1:44pm UTC](https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167/2 "2018-02-28T13:44:18Z")

</div>

There’s an easy way to do this :

```auto
async function() {
    console.log("We will wait for 1.5s until printing the next message to the console"); 
    await sleep(1500);
    console.log("1.5s have now elapsed. "); 
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

```

---

<div class="post-metadata">

**Author:** ![Wynand](https://avatars.discourse-cdn.com/v4/letter/w/e68b1a/32.png) [@Wynand](https://forum.freecodecamp.org/u/Wynand)\
**Post date:** [February 28, 2018, 2:01pm UTC](https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167/3 "2018-02-28T14:01:43Z")

</div>

Thank you for your reply.  
I haven’t worked with async functions before, so I have to do some research now

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 28, 2018, 3:16pm UTC](https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167/4 "2018-02-28T15:16:01Z")

</div>

Also note a switch is ideal in this case - you can do (note `load/play` functions commented out so you can just run this in the browser console):

```auto
async function playSeq(seq) {
  for (const code of seq) {
    await sleep(1500);
    soundPlayer(code);
  }
}

function soundPlayer(seqCode) {
  switch (seqCode) {
    case 0:
      console.log('red');
      // soundRed.load();
      // soundRed.play();
      break;
    case 1:
      console.log('blue');
      // soundBlue.load();
      // soundBlue.play();
      break;
    case 2:
      console.log('green');
      // soundGreen.load();
      // soundGreen.play();
      break;
    case 3:
      console.log('yellow')
      // soundYellow.load();
      // soundYellow.play();
      break;
  }
}

```

---

<div class="post-metadata">

**Author:** ![br3ntor](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/br3ntor/32/7392_2.png) [@br3ntor](https://forum.freecodecamp.org/u/br3ntor)\
**Post date:** [March 1, 2018, 1:43am UTC](https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167/5 "2018-03-01T01:43:34Z")

</div>

This is what I came up with. The above solutions are probably better but this is a way to iterate through an array with a delay.

```auto
const arr = [1, 2, 3, 4, 5];
let count = 0;

function slowCycle() {
    console.log(arr[count]);
    count += 1;
    if (count < arr.length) {
        setTimeout(slowCycle, 1500);
    }
}

// slowCycle(); // Starts immediately
setTimeout(slowCycle, 1500); // Delay on start

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:55pm UTC](https://forum.freecodecamp.org/t/for-loop-with-a-delay/177167/6 "2021-01-16T17:55:43Z")

</div>

Closed on January 16, 2021, 5:55pm UTC


