Skip to content

Instantly share code, notes, and snippets.

@diestrin
Created September 12, 2013 22:38
Show Gist options
  • Save diestrin/6544755 to your computer and use it in GitHub Desktop.
Save diestrin/6544755 to your computer and use it in GitHub Desktop.
waitFor: function (config) {
// Optional
var context = config.context || window,
interval = config.interval || 50,
timeOut = config.timeOut || 2000,
expire = config.expire || function(){};
// Required
var varName = config.varName,
callback = config.callback;
// Local
var timeOutId,
expireId;
if (varName in context && !$.isEmptyObject(context[varName])) {
callback(context[varName]);
} else {
!function lookUp () {
timeOutId = setTimeout(function () {
if (varName in context && !$.isEmptyObject(context[varName])){
callback(context[varName]);
clearTimeout(expireId);
} else {
lookUp();
}
}, interval);
}();
if (config.timeOut !== 0)
expireId = setTimeout(function () {
clearTimeout(timeOutId);
expire();
}, timeOut);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment