I have the following JavaScript function.
function ready(interval, last_wait, el, callback) {
if (jQuery(el).length) {
console.log("Element ready: " + el)
setTimeout(function () {
callback(jQuery(el));
}, last_wait);
} else {
console.log("Waiting element: " + el)
setTimeout(function () {
ready(interval, last_wait, el, callback);
}, interval);
}
};
It basically waits for a DOM element to exist. If it isn't, waits more interval milliseconds.
What I want is adding a timeout parameter. If the function wait timeout milliseconds in total and the DOM element still doesn't appear, then just do nothing, stop waiting and return. I can't make it work because of the recursive nature of the function. I also considered using a global variable, but it seems not the correct way to do it. Hope someone could point me in the right direction.