Last active
February 1, 2021 21:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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