portfolio | contact
February
Saturday the 25th

defer code execution till jQuery plugin is loaded

Saturday, 5:5 PM
On a big enough application, you'll have 5-10 jQuery plugins. if you're lazy loading JS files as necessary, you can't always be certain that the plugins you need are ready to go.

I wrote a quick and dirty method to defer your code till your plugins are ready.
pluginReady = function(plugin){
if(!jQuery.Deferred){
return;
}

// return a deffered object that will resolve when the plugin is available.
var pluginDef = jQuery.Deferred(),
pluginInterval = window.setInterval(function () {
if (!!jQuery[plugin]) {
pluginDef.resolve();
}}, 300),

// give up after 10 seconds
pluginTimeout = window.setTimeout(function (){
window.clearInterval(pluginInterval);
}, 10 * 1000);

jQuery.when(pluginDef).then(function () {
window.clearInterval(pluginInterval);
});

return pluginDef.promise();

};

◄ back