Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save molotow11/4814c349f24ac616c77559dbcc3b9d74 to your computer and use it in GitHub Desktop.
Save molotow11/4814c349f24ac616c77559dbcc3b9d74 to your computer and use it in GitHub Desktop.
Load script and wait for ready
YoutubeDefer.loadAPIVideo(block) {
if(typeof YT === 'undefined') {
loadYoutubeScript();
checkIfYoutubeIsReady().then(function() {
let player = new Vimeo.Player(iframe);
player.play();
});
}
else {
let player = new Vimeo.Player(iframe);
player.play();
}
}
const YoutubeDefer.checkIfYoutubeIsReady = () => {
var wait;
var timeout;
var deferred = new Promise((resolve, reject) => {
wait = setInterval(function() {
if (!Vimeo) {
return;
}
clearInterval(wait);
clearTimeout(timeout);
resolve();
}, 500);
timeout = setTimeout(function() {
clearInterval(wait);
reject();
}, 4000); // subjective. test up to 8 times over 4 seconds
});
return deferred;
}
const YoutubeDefer.loadYoutubeScript = () => {
let s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://www.youtube.com/iframe_api";
document.querySelector("head").append(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment