Skip to content

Instantly share code, notes, and snippets.

@buu700
Forked from BrockA/waitForKeyElements.js
Created December 4, 2012 04:37
Show Gist options
  • Save buu700/4200601 to your computer and use it in GitHub Desktop.
Save buu700/4200601 to your computer and use it in GitHub Desktop.
jQuery plugin which runs handler function once specified element is inserted into the DOM
(function ($) {
/**
* @function
* @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
* @param {function} handler A function to execute at the time when the element is inserted
* @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
* @example $(selector).waitUntilExists(function);
*/
$.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
var found = 'found';
var $this = $(this.selector);
var $elements = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);
if (!isChild)
{
(window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500)
;
}
else if (shouldRunHandlerOnce && $elements.length)
{
window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
}
return $this;
}
}(jQuery));
@rpoddighe
Copy link

I love you. Not a pretty fix in my case but it does the trick.

@diegommarino
Copy link

Thank you so much! That's perfect!

@360disrupt
Copy link

Exactly what I'm looking for.

@tonyporto
Copy link

Just had to stop by and say Thanks man, this is the only plugin that worked for me, nice clean, simple.. Good work

@ChristianLuxem
Copy link

This is not working anymore in Jquery3, because the .selector function was removed.

@ChaosPredictor
Copy link

Any suggestion for Rails 5?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment