Skip to content

Instantly share code, notes, and snippets.

@aFarkas aFarkas/asyncready.js
Last active Jan 3, 2016

Embed
What would you like to do?
<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
You can’t perform that action at this time.