11

so I am making a simon says game. This function displays the current sequence. The problem with it right now is that it doesn't really go in a nice sequence, it kind of does everything at once. Say the colors are "blue", "red", and "yellow, they will all go off at the same time rather than in sequence. How can I fix this?

var displaySequence = function(){
    compSequence.forEach(function(color){
        $("#" + color).fadeTo(300, 0.5).fadeTo(300, 1.0);
    })
}
1
  • You can use setTimeout() to stagger the display of each color in the sequence. Commented Apr 19, 2016 at 1:28

2 Answers 2

28

A none jQuery solution. You will need to use the array index to give the illusion of waiting between each call, however each function has ran already. What will happen is: show color 1 in 1 second, show color 2 in 2 seconds...

var displaySequence = function(){
    compSequence.forEach(function(color, index){
        setTimeout(function(){
            $("#" + color).fadeTo(300, 0.5).fadeTo(300, 1.0);
        },
        1000 * index);
    })
}

adjust the 1000 * index to change the delay.

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

3 Comments

So you say this is non Jquery solution??
No more jQuery solution :)
this isn't supposed to work smh : scottiestech.info/2014/07/01/…
1

I make use of the jQuery delay function

Here is a the javascript.

$(document).ready(function(){

  var compSequence = new Array("red", "blue", "yellow");

  var displaySequence = function() {

      $.each(compSequence, function (i, color) {
        // add delay
        $("#" + color).delay(i * 1000).fadeTo(300, 0).fadeTo(300, 1.0);
      });      
  }

  displaySequence();

});

Here is a DEMO

2 Comments

This works really well but once a duplicate color is added, it is delayed by a huge amount.
@Scriptomaniac, yes, since i = starts 0 to end of the array length, and the delay is multiplied by 1000 milisecs(1sec), to reduce the delay then you can adjust the multiplier value

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.