Skip to content

Instantly share code, notes, and snippets.

@manix84
Created November 30, 2012 12:32
Show Gist options
  • Save manix84/4175508 to your computer and use it in GitHub Desktop.
Save manix84/4175508 to your computer and use it in GitHub Desktop.
Checking for dependancies and sending a callback when they have loaded. For use when you're expecting a library to load and you don't know when it'll appear.
(function () {
var utils = window.utils || {};
utils.dependancyCheck = function (dependancies, callback) {
dependancies = !(dependancies instanceof Array) ? [dependancies] : dependancies;
var checkForDependancies = function () {
var i = 0;
for (; i < dependancies.length; ++i) {
if (dependancies[i] !== undefined) {
// Remove current item from array, then step backwards. Yes I know --VAR and VAR-- differences.
dependancies.splice(i--, 1);
}
if (dependancies.length !== 0) {
window.setTimeout(checkForDependancies, 50);
} else {
callback();
}
}
};
checkForDependancies();
};
window.utils = utils;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment