-
-
Save cargix1/6e79f06b4017d4fbd47b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
* @property {object} jQuery plugin which runs handler function when specified element is inserted into the DOM | |
* @param {function} handler | |
* @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation | |
* @example $('div.comments').waitUntilExists(commentCallbackFunction || function () { alert('function does not exist.'); }) | |
*/ | |
$.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) { | |
var found = 'found'; | |
var $this = $(this.selector); | |
var $elements = $this.not(function () { return $(this).data(found); }); | |
$elements.each(function () { handler(this); $(this).data(found, true); }); | |
if (!isChild) | |
{ | |
window.waitUntilExists_Intervals = 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment