Created
December 30, 2017 09:34
-
-
Save RichardDillman/3a04b9cdc6810cfdfb6cb3623107558a to your computer and use it in GitHub Desktop.
Checks for an elements existence within a RAF within a promise. Raw
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
/** | |
* Checks for an elements existence within a RAF within a promise. | |
* | |
* @param {string} selector - The element you wish to find. Defaults to 'body'. | |
* @param {string} target - The parent element to search within. Defaults to document. | |
* @return {Promise} Resolves when the element exists. | |
*/ | |
function elementReady(selector, target) { | |
var options = { | |
target: target || document, | |
selector: selector || 'body' | |
} | |
return new Promise(function promise(resolve) { | |
(function check() { | |
var el = options.target.querySelector(options.selector); | |
if (el) { | |
resolve(el); | |
} | |
requestAnimationFrame(check); | |
})(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment