-
-
Save PizzaBrandon/5709010 to your computer and use it in GitHub Desktop.
| ;(function ($) { | |
| /** | |
| * @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 $(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 (handler === "remove" || (shouldRunHandlerOnce && $elements.length)) | |
| { | |
| window.clearInterval(window.waitUntilExists_Intervals[this.selector]); | |
| } | |
| return $this; | |
| } | |
| }(jQuery)); |
- Further improved compressibility (by one byte) by passing "window" into the function.
see my BUGFIX https://gist.github.com/md55/6565078
Brandon: Could you please specify whether this code exists under a particular license or is in the public domain?
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.
Ok, thank you.
Can you add to your work an html working example with a delayed item?
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)
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.
Hello! Would you be so kind to provide a license? It would be really helpful for people which want to use it in enterprises.
Hey, great plugin! Do you mind providing a liscence with it? Would really help know when and where I can use it. Thanks!
Cool
Updates from the original version: