Skip to content

Instantly share code, notes, and snippets.

@Pandiora
Forked from chrisjhoughton/wait-el.js
Last active December 7, 2022 10:29
Show Gist options
  • Save Pandiora/7b002ac074430bd7197b009ec2440ff0 to your computer and use it in GitHub Desktop.
Save Pandiora/7b002ac074430bd7197b009ec2440ff0 to your computer and use it in GitHub Desktop.
Wait for an element to exist on the page with jQuery
// altered to version like described in comments and passing back selector
// to be used later with that or whatever var one chooses
// maxTimes should be used to not have a neverending timeout loop
const waitForEl = (selector, maxTimes = false, callback) => {
// wait for selector until it exists and pass it back
if (jQuery(selector).length) {
callback(selector);
} else {
if (maxTimes === false || maxTimes > 0) {
(maxTimes != false) && maxTimes-- ;
setTimeout(function () {
waitForEl(selector, maxTimes, callback);
}, 100);
}
}
};
waitForEl(selector, maxTimes, (that)=>{});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment