I am building a Simon game, and have built a function for each new round as so:
var game = [];
var squares = ["green", "red", "blue", "yellow"];
var newRound = function() {
// adds a new round to the end of the game array
game.push(squares[Math.floor(Math.random() * squares.length)]);
// for loop to run through the game array
for (var x = 0; x < game.length; x++) {
playButton(game[x]);
}
}
Then, I built another function which controls the animation and sound for each time the square is either hit by a user, or cycled through my for-loop
var playButton = function(color){
$("#"+color).addClass(color+"--active active", 300, function(){
$("#audio-"+color).trigger('play');
$("#"+color).removeClass(color+"--active active", 300)
});
Right now, my for-loop just cycles through all the animations and sounds in one go. How can I make the for-loop wait for the playButton function to finish execution before cycling through again?