I combine the use of the YouTube iFrame API and jQuery, loaded via a script tag that has a defer flag. The grace flag should be set, as the client has an ideal reading rate on the Google page and wants to keep this score.
The YouTube API, when fully loaded and ready to use, instantly calls the function that I define onYouTubeIframeAPIReady . Then it will call onPlayerReady when the player is fully loaded and displayed.
I want to use jQuery in this function, but just using jQuery inside the onPlayerReady function will create a race condition (hoping that the jQuery library will finish loading by the time onPlayerReady is called).
It seemed to me that a workable solution would be to use the onPlayerReady function to set a variable before calling a function that checks both the player and jQuery. Another function sets the variable when jQuery is ready and calls the same test function.
I have code that works, but the part that checks jQuery seems messy for me, and also introduces a little extra extra delay. I was wondering if anyone knows of a better way to run something when instant jQuery becomes available. Basically, are there any jQuery callbacks that become available built into the library?
My current code is as follows:
var ready = { 'jquery': false, 'youtube' false }, testJQueryLoaded; testJQueryLoaded = function() { if(typeof jQuery == 'undefined') { window.setTimeout(function() { testJQueryLoaded(); }, 25); return; } ready.jquery = true; postLibraryLoad(); }; testJQueryLoaded(); function onYouTubeIframeAPIReady() {
Scots source share