Created
June 8, 2021 05:35
-
-
Save molotow11/4814c349f24ac616c77559dbcc3b9d74 to your computer and use it in GitHub Desktop.
Load script and wait for ready
This file contains hidden or 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
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