Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Updated waitUntilExists plugin
;(function ($, window) {
var intervals = {};
var removeListener = function(selector) {
if (intervals[selector]) {
window.clearInterval(intervals[selector]);
intervals[selector] = null;
}
};
var found = 'waitUntilExists.found';
/**
* @function
* @property {object} jQuery plugin which runs handler function once specified
* element is inserted into the DOM
* @param {function|string} handler
* A function to execute at the time when the element is inserted or
* string "remove" to remove the listener from the given selector
* @param {bool} shouldRunHandlerOnce
* Optional: if true, handler is unbound after its first invocation
* @example jQuery(selector).waitUntilExists(function);
*/
$.fn.waitUntilExists = function(handler, shouldRunHandlerOnce, isChild) {
var selector = this.selector;
var $this = $(selector);
var $elements = $this.not(function() { return $(this).data(found); });
if (handler === 'remove') {
// Hijack and remove interval immediately if the code requests
removeListener(selector);
}
else {
// Run the handler on all found elements and mark as found
$elements.each(handler).data(found, true);
if (shouldRunHandlerOnce && $this.length) {
// Element was found, implying the handler already ran for all
// matched elements
removeListener(selector);
}
else if (!isChild) {
// If this is a recurring search or if the target has not yet been
// found, create an interval to continue searching for the target
intervals[selector] = window.setInterval(function () {
$this.waitUntilExists(handler, shouldRunHandlerOnce, true);
}, 500);
}
}
return $this;
};
}(jQuery, window));
(function(e,f){var b={},g=function(a){b[a]&&(f.clearInterval(b[a]),b[a]=null)};e.fn.waitUntilExists=function(a,h,j){var c=this.selector,d=e(c),k=d.not(function(){return e(this).data("waitUntilExists.found")});"remove"===a?g(c):(k.each(a).data("waitUntilExists.found",!0),h&&d.length?g(c):j||(b[c]=f.setInterval(function(){d.waitUntilExists(a,h,!0)},500)));return d}})(jQuery,window);
@PizzaBrandon

This comment has been minimized.

Copy link
Owner Author

commented Jun 4, 2013

Updates from the original version:

  • BUGFIX: Ensure "shouldRunHandlerOnce" is respected. If the target was found right away, an interval would still be created and run continuously
  • Enable removing the listener from the given selector by passing in "remove" as the handler
  • Move intervals object out of window namespace and into closure
  • Added explanatory inline documentation
  • Improved overall compressibility
@PizzaBrandon

This comment has been minimized.

Copy link
Owner Author

commented Jun 5, 2013

  • Further improved compressibility (by one byte) by passing "window" into the function.
@md55

This comment has been minimized.

Copy link

commented Sep 15, 2013

@codedr1

This comment has been minimized.

Copy link

commented Sep 16, 2014

Brandon: Could you please specify whether this code exists under a particular license or is in the public domain?

@PizzaBrandon

This comment has been minimized.

Copy link
Owner Author

commented Sep 18, 2014

My additions of the code are public, but you'll have to look up the fork chain to see if there are any licenses encumbering the previous work.

@codedr1

This comment has been minimized.

Copy link

commented Sep 19, 2014

Ok, thank you.

@erm3nda

This comment has been minimized.

Copy link

commented Jan 15, 2015

Can you add to your work an html working example with a delayed item?

@jakobjp

This comment has been minimized.

Copy link

commented Jul 30, 2015

Would it be possible to incorporate some further settings?

  • Time length between attempts
  • Number of maximum attempts
  • Callback for timeout (max attempts tried without finding element)
@PizzaBrandon

This comment has been minimized.

Copy link
Owner Author

commented Mar 9, 2016

I've left this code here for anyone to reference, but I no longer maintain or add features to it. You are more than welcome to.

@dikeert

This comment has been minimized.

Copy link

commented May 19, 2016

Hello! Would you be so kind to provide a license? It would be really helpful for people which want to use it in enterprises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.