I created an array that stores the ids of buttons. Button ids are #red
, #green
, #blue
and #yellow
I created another function the randomly selects a color and stores it inside of another array.
I am tying to iterate the second array using a for
loop and use a fade in/out effect on the buttons so the outcome will be an ordered fade in and out effect.
For example:
array = ['red','green','blue'];
First the red button fades in and out then the green and lastly the blue.
The outcome I get is a fade in and out effect almost at the same time. Can anybody please provide a solution and tell me why it happens?
var buttonColours = ["red", "blue", "green", "yellow"];
var GamePattern = [];
function nextSequence() {
var randomNumber = Math.floor((Math.random() * 4));
var randomChosenColour = buttonColours[randomNumber]
GamePattern.push(randomChosenColour);
}
function playSequence(sequence) {
for (var i = 0; i < sequence.length; i++) {
$("#" + sequence[i]).fadeOut(1000).fadeIn(1000)
}
}
nextSequence()
nextSequence()
nextSequence()
playSequence(GamePattern)
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
<h1 id="level-title">Press A Key to Start</h1>
<div class="container">
<div lass="row">
<div type="button" id="green" class="btn green"></div>
<div type="button" id="red" class="btn red"></div>
</div>
<div class="row">
<div type="button" id="yellow" class="btn yellow"></div>
<div type="button" id="blue" class="btn blue"></div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>