Created
June 9, 2014 19:16
-
-
Save aaronchi/1d602b4c513624b70146 to your computer and use it in GitHub Desktop.
waitFor without deffered
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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