Skip to content

Instantly share code, notes, and snippets.

@aaronchi
Created June 9, 2014 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronchi/1d602b4c513624b70146 to your computer and use it in GitHub Desktop.
Save aaronchi/1d602b4c513624b70146 to your computer and use it in GitHub Desktop.
waitFor without deffered
(function ($) {
'use strict';
$.fn.waitFor = function (options) {
options = options || {}; // Note: all arguments default
options.timeout = options.timeout || 100; // Note: default timeout
var el;
var selector = this.selector;
function waitForElement(timeout) {
var $elements = $(selector);
if ($elements.length > 0) {
el = $elements;
} else {
var waitTime = 50;
if (timeout < waitTime) {
throw('Timed out waiting for: "' + selector + '"');
} else {
window.setTimeout(function () {
waitForElement(timeout - waitTime);
}, waitTime);
}
}
}
function startWaiting() {
waitForElement(options.timeout);
}
if (options.pause) {
window.setTimeout(startWaiting, options.pause);
} else {
startWaiting();
}
return el;
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment