Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Created December 9, 2012 22:12
Show Gist options
  • Save nicerobot/4247243 to your computer and use it in GitHub Desktop.
Save nicerobot/4247243 to your computer and use it in GitHub Desktop.
waitUntilExists

This through lint and minified.

(function(){var e={pendingFunctions:[],loopAndCall:function(){if(!e.pendingFunctions.length)return;for(var t=0;t<e.pendingFunctions.length;t++){var n=e.pendingFunctions[t],r=document.getElementById(n.id);n.id===document&&(r=document.body);if(r){var i=n.f;e.pendingFunctions.splice(t,1);n.c==="itself"&&(n.c=r);i.call(n.c);t--}}},globalInterval:setInterval(function(){e.loopAndCall()},5)};if(document.addEventListener){document.addEventListener("DOMNodeInserted",e.loopAndCall,!1);clearInterval(e.globalInterval)}var t=function(t,n,r){r=r||window;if(typeof t=="function"){r=n;n=t;t=document}e.pendingFunctions.push({f:n,id:t,c:r})};t.stop=function(t,n){for(var r=0;r<e.pendingFunctions.length;r++)e.pendingFunctions[r].id===t&&(typeof n=="undefined"||e.pendingFunctions[r].f===n)&&e.pendingFunctions.splice(r,1)};t.stopAll=function(){e.pendingFunctions=[]};window.waitUntilExists=t})();
/*
* Wait Until Exists Version v0.2 - http://javascriptisawesome.blogspot.com/
*
*
* TERMS OF USE - Wait Until Exists
*
* Open source under the BSD License.
*
* Copyright © 2011 Ivan Castellanos
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the author nor the names of contributors may be used to endorse
* or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
(function() {
var _waitUntilExists = {
pendingFunctions : [],
loopAndCall : function() {
if(!_waitUntilExists.pendingFunctions.length) {
return;
}
for(var i=0;i<_waitUntilExists.pendingFunctions.length;i++) {
var obj = _waitUntilExists.pendingFunctions[i];
var resolution = document.getElementById(obj.id);
if(obj.id === document) {
resolution = document.body;
}
if(resolution) {
var _f = obj.f;
_waitUntilExists.pendingFunctions.splice(i, 1);
if(obj.c === "itself") {
obj.c = resolution;
}
_f.call(obj.c);
i--;
}
}
},
globalInterval : setInterval(function() {_waitUntilExists.loopAndCall();},5)
};
if(document.addEventListener) {
document.addEventListener("DOMNodeInserted", _waitUntilExists.loopAndCall, false);
clearInterval(_waitUntilExists.globalInterval);
}
var waitUntilExists = function(id,theFunction,context) {
context = context || window;
if(typeof id === "function") {
context = theFunction;
theFunction = id;
id=document;
}
_waitUntilExists.pendingFunctions.push({
f:theFunction,
id:id,
c:context
});
};
waitUntilExists.stop = function(id,f) {
for(var i=0;i<_waitUntilExists.pendingFunctions.length;i++) {
if(_waitUntilExists.pendingFunctions[i].id === id &&
(typeof f === "undefined" ||
_waitUntilExists.pendingFunctions[i].f === f)) {
_waitUntilExists.pendingFunctions.splice(i, 1);
}
}
};
waitUntilExists.stopAll = function() {
_waitUntilExists.pendingFunctions = [];
};
window.waitUntilExists = waitUntilExists;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment