function waiter(selector, callback, timeout) { var elem = document.querySelectorAll(selector); if (!elem.length) { if (timeout === undefined) { timeout = 100; } setTimeout(function(){ waiter(selector, callback, timeout); }, timeout); } else { if (callback) { callback(elem); } } }