Help on the Simon Project: Same Color (light) only shows ONCE? - Callback of Timeout() function?

Problem: I am generating a sequence of light ups using a random function. Now the problem is if same lights come up in a continuous sequence, they only fire once. So if a sequence is green, red, red, blue; the red will only light up once. (Note: I am generating a random number between 1 to 4 using Math.random() function, and storing them in the array sequence; then ‘light up’ lights according to 1 = green, 2= red, 3 = yellow, 4 = blue)

This is my Simon Game Project:

You have to use some setTimeout() to get interval between the loop of light ups , i found a solution on this stackFlow answer

edit: oh yea i forgot to add here is my progress so far, it is not finished im still working on the user interface/experience

1 Like

@AndreiCalazans Yes, I am using setTimeout() and it is even working as long as two consecutive light ups are not of the same color. When I try to light up the same color twice consecutively it lights up once only

Hi @verv0eren

I think I figured out the problem.

If you look at the console output below (I added a few console statements to your light-up functions), you can see that the on command for the next item in the sequence, is being called BEFORE the off command of the current item in the sequence.

This gives the appearance that it’s not doing anything, however, it is reapplying the on color (to the third one in this case) again just before it’s turned off.

I reduced the setTimeout delay inside your lightup functions from 1000 to 900, which fixed the issue. You may want to play around with the timings though so it doesn’t flash so quickly.

seq- 3
third on.
third off.
seq: 3
reg: 3
seq- 3,3
third on.
third on.
third off.
third off.

Thank you very much for looking into this and helping me out. Yes, 900 seems to work! Now I got the direction where to experiment. That registering on and off is also a really good idea. Why didn’t I think about it, :relaxed:?

No worries dude :grinning: