Last active
January 3, 2016 21:08
-
-
Save aFarkas/8519169 to your computer and use it in GitHub Desktop.
<script async="">
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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