Skip to content

Instantly share code, notes, and snippets.

@tdshipley
Created April 24, 2018 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdshipley/6c09428edb887ae06fa4bfdc687de2e6 to your computer and use it in GitHub Desktop.
Save tdshipley/6c09428edb887ae06fa4bfdc687de2e6 to your computer and use it in GitHub Desktop.
Wait for jQuery to load
async function checkJquery(tries, tryLimit) {
console.log("Waiting for jQuery to Load");
//1. Base Case - JQuery Loaded
if (window.jQuery) {
console.log("jQuery Loaded");
return;
}
//2. Base Case - TryLimit Hit
if(parseInt(tries) > parseInt(tryLimit)) {
console.log("jQuery Failed to Load");
return;
}
//3. Recursive Case
console.log("Could not find jQuery trying again. Try number " + tries + " of " + tryLimit);
// Wait a second.
var wait = ms => new Promise((r, j)=>setTimeout(r, ms));
await wait(1000);
// Try again.
tries = parseInt(tries) + 1;
checkJquery(tries, tryLimit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment