Skip to content

Instantly share code, notes, and snippets.

@casecode
Last active August 29, 2015 14:23
Show Gist options
  • Save casecode/3ad35d1f454b7787a710 to your computer and use it in GitHub Desktop.
Save casecode/3ad35d1f454b7787a710 to your computer and use it in GitHub Desktop.
var poll = function (sel, fn, t) {
t = t || 25; // 25ms default
var $el = $(sel), found = $el.length;
// Proper tail calls - return and pass all necessary variables
// i.e. minimize current frame's footprint (allow release / reuse in es6 env)
if (found) return fn.call($el);
else if (document.readyState === 'complete') return;
else return setTimeout(function() {
poll(sel, fn, t);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment