Reputation: 193
I have 2 audio files, this.audio1 and this.audio2. For each trial, the user is supposed to choose whether the two files are the same or different. So, I want to play the two audio files one after each other, wait for the use to click the same or different button, and then go to the next trial. This is my code so far for playing the audio files:
for (var i = 0; i < this.numTrials; i++){
var audio1; // these are actually initialized, obviously, in my code
var audio2;
var noRepeat = true;
audio1.addEventListener('ended', function () {
if (noRepeat) {
audio2.play();
noRepeat = false;
}
});
audio2.addEventListener('ended', function () {
game.audioComplete = true;
});
audio1.play();
// here, I want to add code so it waits until a button is pressed before moving on
}
I tried an eventListener where my comment is, which didn't work (it just kept looping through anyway). Does anyone have any suggestions for what I could use to "pause" the loop and wait for the button press, and then have it move on?
Thanks!
Upvotes: 0
Views: 48
Reputation: 601
Something like this, this is the same as what @fixatd commented.
var globalPlayCount = 0;
...
function play() {
if(globalPlayCount < this.numTrials) {
globalPlayCount++:
audio1.play();
}
}
...
Fire play()
on your button click. Just make sure you have both your event listeners intact.
Upvotes: 1