I am still a JS beginner, trying to get to grips with jQuery and started coding up the Simon Game as a project. You have 4 colored spaces that should light up & play a sound in a random sequence, starting with a sequence of length 1. The player recreates that sequence by clicking on the squares. If successful the random sequence length will be incremented by 1, if you make a mistake you have to start over.
I am at the stage of simply creating that random sequence of length 5 and visually showing the sequence. That's what I've come up with:
var sequence = [];
$("button").click(startGame);
function startGame() {
for (let index = 0; index < 5; index++) {
sequence.push(nextColour());
touchField(index);
// playSound();
}
}
function nextColour() {
var randomNumber = Math.floor(Math.random() * 4) + 1;
if (randomNumber === 1) {
var nextColour = "red";
} else if (randomNumber === 2) {
var nextColour = "blue";
} else if (randomNumber === 3) {
var nextColour = "green";
} else {
var nextColour = "yellow";
}
return nextColour
};
function touchField(index) {
$("." + sequence[index]).addClass("active");
setTimeout(function() {
$("." + sequence[index]).removeClass("active")
}, 300);
}
.active {
border: 1rem solid purple;
}
.red {
border: 1px solid red;
}
.blue {
border: 1px solid blue;
}
.green {
border: 1px solid green;
}
.yellow {
border: 1px solid yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="red" src="https://via.placeholder.com/50" alt="">
<img class="blue" src="https://via.placeholder.com/50" alt="">
<img class="green" src="https://via.placeholder.com/50" alt="">
<img class="yellow" src="https://via.placeholder.com/50" alt="">
So the issue is, that the class ".active" gets added and removed from all the squares almost simultaneously - which makes it impossible to distinguish any kind of sequence. So I thought I can use "setTimeout();". That does not prevent it from doing it though. I believe what happens is, that until the expression which is to be evaluated after the timeout is indeed executed, the code simply keeps on running, thus adding the ".active" and the time out 5 times almost simultaneously - which leads to their almost simultaneous removal. Ideally there is sufficient time between removing ".active" and adding ".active" to the next square. I also tried adding setTimeout to the "touchSquare()" function call. It doesn't work either - I believe for the same reason, it just keeps executing. I hope you see my problem. ^^
I have looked at other questions, but the answers seem to disregard the fact that the browser keeps executing code after it got to the setTimeout, like here for example:
How to delay execution in between the following in my javascript
If you copy paste that into the console, it does not at all do what it is supposed to exactly because the code keeps being executed after the setTimeout is recognised. I hope I could make my problem make sense to you, it's the first time I am posting a question here on StackOverflow. If I can improve the way I ask a question in any way I am very grateful for constructive criticism!