This snippet stops or pauses YouTube, Vimeo and HTML5 videos when the content area is closed. If you’re interested in adding something like that to your scripts, here’s how it works.
Add this method to your script:
var stopVideo = function ( element ) {
var iframe = element.querySelector( 'iframe');
var video = element.querySelector( 'video' );
if ( iframe !== null ) {
var iframeSrc = iframe.src;
iframe.src = iframeSrc;
}
if ( video !== null ) {
video.pause();
}
};
Call the function as needed, passing the container element into the function:
stopVideo( tab );
Source
https://gomakethings.com/stopping-youtube-vimeo-and-html5-videos-with-javascript/