I'm working on a "Simon Says" game, and I would like to make a div active and play a sound for the same duration as the div is active. Then I want to remove the class and stop the sound, then move on to next div.
So far I've worked out how to add a class for a specific period of time; however, how can I play the sound for that same amount of time? This functionality is part of the simonSequence()
function. Thanks.
Code:
//variables
var validMov;
var min = 0, max = 4;
var boardSeq = [];
var userSeq = [];
var boardSound = {
red: "http://www.soundjay.com/button/sounds/button-09",
blue: "http://www.soundjay.com/button/sounds/button-7",
yellow: "http://www.soundjay.com/button/sounds/button-10",
green: "http://www.soundjay.com/button/sounds/button-4"
}
$(document).ready(function() {
})
//1- board plays first generating random numbrer from 0 to 3 (4)
function simonSequence() {
var random = Math.floor(Math.randon() * (max + min));
boardSeq.push(random);
for(var i = 0; i < boardSeq.length; i++) {
$("#"+boardSeq[i]).addClass("active");
setTimeout(function() {$("#"+boardSeq[i]).removeClass("active")}, 500);
}
}
//2- user plays repeating game sequence.
//3- compare computer sequence with user and update count/display
Pen: