I have 4 buttons that each play their own music when pressed. I needed something that pauses one music when another button is clicked so that I dont have all 4 music playing on top of each other. I found this code to solve it:
document.addEventListener('play', function(event){
var allAudios = document.getElementsByTagName('audio');
debugger;
for(var i = 0; i<allAudios.length; i++){
if(allAudios[i] != event.target){
allAudios[i].pause();
}
}
}, true);
However, I'm not understanding the exact purpose of the true
at the end. I understand its telling the event listener to use capture event but when I set up an alert on the event target's parents, the order of events gets triggered up from the event target to its parents (bubbling) and parents to event target (capturing). Does anyone have an explanation to this phenomena and why useCapture is needed in this case to begin with?
I got the code from this website if anyone needs a reference.
https://teamtreehouse.com/community/auto-stop-when-playing-other-files
Thank you