Skip to content

Instantly share code, notes, and snippets.

@arigesher
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arigesher/8930705 to your computer and use it in GitHub Desktop.
Save arigesher/8930705 to your computer and use it in GitHub Desktop.
A dirty-but-effective method for getting inline script blocks to wait for a load of the JQuery library so they can use the superior API it offers. See http://ari.gesher.net/photos-the-gesher-world-tour/ for working example.
var waitForJquery = function(jqueryCallback, interval) {
if(typeof $ === 'function') {
jqueryCallback();
} else {
setTimeout(function() {
if(typeof $ === 'function'){
jqueryCallback();
} else {
waitForJquery(jqueryCallback, interval);
}
}, interval);
}
}
waitForJquery(function() {
$('#dostuff');
}, 50); // poll for jquery every 50ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment