Skip to content

Instantly share code, notes, and snippets.

@imyelo
Last active August 29, 2015 13:58
Show Gist options
  • Save imyelo/10251164 to your computer and use it in GitHub Desktop.
Save imyelo/10251164 to your computer and use it in GitHub Desktop.
wait window[obj]
var wait = function (name, context) {
context = context || window;
var isReady = false;
var tasks = [];
var when = function (callback) {
if (isReady) {
return callback();
}
return tasks.push(callback);
};
var check = function (next) {
var again = function () {
console.log('check');
setTimeout(function () {
if (typeof context[name] === 'undefined') {
again();
} else {
next();
}
}, 200);
};
again();
};
check(function () {
isReady = true;
while (tasks.length > 0) {
tasks.shift()();
}
});
return when;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment