Created
January 7, 2013 09:48
-
-
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
This file contains hidden or 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
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