/jquery.waituntilexists.js Secret
Created
December 4, 2012 04:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)); |
I forked and improved on your code a bit, if you want to pull in some or all of my changes. Thanks!
Thanks a lot!
Thanks!!!! Immediately solved the problem
Thanks !!
Thank you so much, this solved a very large headache for me!
Brilliant and easy! Thank you for this.
Thank You for this great piece of code !
I love you. Not a pretty fix in my case but it does the trick.
Thank you so much! That's perfect!
Exactly what I'm looking for.
Just had to stop by and say Thanks man, this is the only plugin that worked for me, nice clean, simple.. Good work
This is not working anymore in Jquery3, because the .selector function was removed.
Any suggestion for Rails 5?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thank you for this great plugin. But how it is possible to run the function not only once, but every time.
I used it in the flexslider plugin to find out when a slider is active and then add a special class to the navigation.
my code:
var secondSlide =$("#secondary-slider li:eq(1).flex-active-slide");
var next2 = $('#home .flex-direction-nav li:eq(1) a');
$(secondSlide).waitUntilExists(function() {
next2.addClass('flex-disabled')
});
This works fine, but only at once.