Need help regarding timings of "lights" in the Simon Game

Need help regarding timings of "lights" in the Simon Game
0

#1

hey, guys, I am using the following program for ‘‘lighting up’’ colors in Simon game but what happens is while the first light goes up after 5 seconds, following lights go up immediately, not after the 5 seconds delay.

sequence = [1,2,3,4,1,2,3,4]; // for testing purpose
function seqLightup() {
    for (var i = 0; i < sequence.length; i++) {
      switch (sequence[i]) {
        case 1:
          setTimeout(lightup1, 5000);
          break;
        case 2:
          setTimeout(lightup2, 5000);
          break;
        case 3:
          setTimeout(lightup3, 5000);
          break;
        case 4:
          setTimeout(lightup4, 5000);
      }
    }
  }

whole code is here: https://codepen.io/verv0eren/pen/rjawRO?editors=0010#0


#2

Your sequence runs immediately, and your computer can plow through that for loop in milliseconds. setTimeout does not wait for any other code to finish before it starts its timer, so what you have is a bunch of lights that run 5 seconds after the very fast loop ran the timer function. What you need to do is write code that iterates through an array, doing something to the current item and only moving on to the next when the current operation is done.