Skip to content

Instantly share code, notes, and snippets.

@aFarkas
Last active January 3, 2016 21:08
Show Gist options
  • Save aFarkas/8519169 to your computer and use it in GitHub Desktop.
Save aFarkas/8519169 to your computer and use it in GitHub Desktop.
<script async="">
/*
asyncReady('jQuery', function(fn){
console.log('jQuery found', fn)
});
asyncReady('jQuery.ui', function(fn){
console.log('jQuery found', fn)
});
*/
window.asyncReady = (function(){
var readys = {};
var check = function(){
var globalFound = true;
for(var r in readys){
if(readys[r].c){
for(var i = 0, root = window; i < readys[r].p.length; i++){
if(!(root = root[readys[r].p[i]])){
globalFound = false;
break;
}
}
if(root){
while(readys[r].c.length){
readys[r].c.shift()(root);
}
delete readys[r].c;
}
}
}
if(!globalFound){
setTimeout(check);
console.log('check')
} else {
console.log('stop')
}
};
return function(test, fn){
if(!readys[test]){
readys[test] = {
p: test.split('.'),
c: []
};
}
readys[test].c.push(fn);
check();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment