Inside my correctClick function I'm comparing a button click to an array (buttonPressValidate) that contains all the id's of buttons that were highlighted on the screen. If the button that was highlighted is clicked then replayFlash function highlights all the buttons inside buttonPressValidate and then the highLightSquare function should highLight a new square after the replayFlash function finishes. The problem I'm having is that my highLightSquare function isn't waiting for the replayFlash function to finish before highlighting a new square.
var clickNum = 0;
function correctClick(buttons) {
$(".button").on("click", function() {
var thisClick = $(this).attr("id");
var matchBut = buttonPressValidate[clickNum];
if(thisClick == matchBut) {
clickNum++;
setTimeout(function() {
replayFlash(buttonPress);
if(buttonPressValidate.length === clickNum) {
setTimeout(function() {
highLightSquare(simonButtons);
}, 1500);
}
}, 1500);
}
});
}
function replayFlash(butPressArr) {
function eachColor(i) {
var litColor = $(butPressArr[i]);
setTimeout(function() {
litColor.addClass("lit");
if(litColor.attr("id") === "greenButton") {
greenButton.play();
} else if(litColor.attr("id") === "redButton") {
redButton.play();
} else if(litColor.attr("id") === "blueButton") {
blueButton.play();
} else if(litColor.attr("id") === "yellowButton") {
yellowButton.play();
}
setTimeout(function() {
litColor.removeClass("lit");
}, 1000 - (1000 / 3));
}, 1000 * (i + 1));
}
for(var i = 0; i < butPressArr.length; i++) {
eachColor(i);
}
}