There are several ways you can approach this. The first method is to check the timer for a function or variable provided by a third-party script. If it is not available when you check it, it is not loaded yet:
$(document).ready( function() { var extScript = document.createElement('script'); extScript.type = 'text/javascript'; extScript.src = 'http://third-party.com/scriptfile.js'; window.setTimeout(function () { if ("thirdPartyFuncName" in window) alert("loaded"); else alert("Not loaded yet"); }, 10000);
Another way you could go is to investigate the onload and onreadystatechange events provided by browsers. AFAIK, onload widely used, so you can do something like this:
var timer; extScript.onload = function () { clearTimeout(timer); } timer = window.setTimeout(function () { alert("not loaded"); }, 10000);
The disadvantage of this approach is that the load event will fire even if the script has a syntax error, so I still think that the first approach is your best choice, because it checks for the existence of functionality.
source share