My code:
I understand that my for loop assigns all array elements to the variable pickSound and that is why I am left with it only playing the last element. So how can I get it to play each element in order and start over once done.
function show() {
var sounds = new Array(
"audio/basement.mp3",
"audio/roll.mp3",
"audio/gatorade.mp3",
"audio/half.mp3",
"audio/hotdogs.mp3",
"audio/keys.mp3",
"audio/heil.mp3",
"audio/money.mp3",
"audio/ours.mp3",
"audio/pass.mp3"
);
for (var i = 0; i < sounds.length; i++){
var pickSound = sounds[i];
}
$('#divOne').html("<embed src=\""+ pickSound +"\" hidden=\"true\" autostart=\"true\" />");
return false;
};
I take it that means the sounds aren’t to be played continuously on an automatic loop. You intend for a click of a button to play whichever sound is next and then stop?
In the following code the
nextSoundvariable holds the index of whatever sound should be played next. When a button is clicked (insert your button’s ID or other selector as appropriate) the file name associated with that index is used and thennextSoundis incremented using the modulus operator to loop back to zero when it gets to the end of the array.Note also that it is generally recommend to declare arrays with the square bracket
[]array literal syntax rather thannew Array().