Simple function to run javascript after specific content is loaded.
jQuery dependant.
Useful for dealing with resizing iframes after content is loaded to avoid scroll bars in the iframe. Also serves as an alternative to waitForKeyElement when tinkering with userscripts et.
Doc:
waitForAsyncElement("#some-data", function(){
console.log($("#some-data").val());
})
waitForAsyncElement("#some-data", function(){
console.log($("#some-data").val());
}, 30, 5000, function(){
console.log("#some-data did not load");
})
onElementLoaded("#iFrame1", function(){
var iFrame = document.getElementById('iFrame1');
iFrame.height = iFrame.contentWindow.document.body.scrollHeight + 100;
}, 300);
Code language: JavaScript (javascript)
Code:
var waitForAsyncElement = function(elementSelector,
callback,
delay = 30,
timeoutms = 5000,
timeoutFallback = function(){return}){
var intervl = setInterval(function(){
if (jQuery(elementSelector).length){
clearInterval(intervl);
setTimeout(() => {callback()}, delay);
}
}, 300);
if (timeoutms !== 0){
setTimeout(() => {
clearInterval(intervl);
timeoutFallback();
}, timeoutms);
}
};
Code language: JavaScript (javascript)
Licence:
Public Domain or MIT, whichever suits you best. However please keep me in the loop if you build on the function, make and improvements and such 🙂