How to implement a "Listener" in javascript for screen viewing?

Asked

Viewed 39 times

0

I need to implement a change in my code so that when I change DIV, IE, stop viewing the page, the sound that is running automatically stops, as already happens with the videos of Facebook/Instagram on web platforms. I made the following code:

<audio id="myAudio" src="../media_a/song3.mp3" preload="auto">
</audio>

<script type="text/javascript" charset="utf-8" async defer>
var myAudio = document.getElementById("myAudio");
var isPlaying = false;

function togglePlay() {
if (isPlaying) {
myAudio.pause()
} else {
myAudio.play();
}
}; 
myAudio.onplaying = function() {
isPlaying = true;
};
myAudio.onpause = function() {
myAudio.currentTime = 0;
isPlaying = false;
};
</script>  

The ON/OFF button is 100% functional, but when I call another DIV, in a single-page system, the audio must stop running, but it continues. Being that each DIV has a different audio description, as I implement a "Istener" so that when leaving to visualize that DIV the sound stops automatically?

  • How are you loading that other page? You could add the code to pause the audio within that function.

  • When you say "change page" would you change tab? That point was not clear.

  • I have a menu that through a script calls the Divs.

  • @Elanio is the same as I asked the question. It is ON/OFF type restarting the audio, only when calling another DIV it keeps playing, and as each DIV has its own audio, it complicates why the audio bulges all over. mixing the sounds.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.