Skip to content

Instantly share code, notes, and snippets.

@MaxPower15
Last active February 1, 2021 21:06
Show Gist options
  • Save MaxPower15/5302de3be48c47ca5a4a23dbc34c1d8c to your computer and use it in GitHub Desktop.
Save MaxPower15/5302de3be48c47ca5a4a23dbc34c1d8c to your computer and use it in GitHub Desktop.
Pause all Wistia videos when tab isn't visible, resume when visible again
var pausedDueToVisibility = [];
function pauseAllActiveWistiaVideos() {
window._wq = window._wq || [];
window._wq.push({
id: "_all",
function (video) {
if (video.state() === "playing") {
pausedDueToVisibility.push(video);
video.pause();
}
}
});
}
function resumePausedWistiaVideos() {
pausedDueToVisibility.forEach(function(video) {
video.play();
});
pausedDueToVisibility = [];
}
function handleVisibilityChange() {
if (document.hidden) {
pauseAllActiveWistiaVideos();
} else {
resumePausedWistiaVideos();
}
}
document.addEventListener('visibilitychange', handleVisibilityChange, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment