Skip to content

Instantly share code, notes, and snippets.

@gary-rafferty
Created January 7, 2013 09:48
Show Gist options
  • Save gary-rafferty/4473760 to your computer and use it in GitHub Desktop.
Save gary-rafferty/4473760 to your computer and use it in GitHub Desktop.
Javascript function that waits for an object to be available, and then invokes with callback
function whenAvailable(name, callback) {
var interval = 10;
window.setTimeout(function() {
if(window[name]) {
callback(window[name]);
} else {
setTimeout(arguments.callee, interval);
}
}, interval);
}
//Sample usage: Facebook JS SDK
whenAvailable('FB',function(fb) {
fb.api(/*Some Facebook API call*/);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment