Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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);
- })();
- });
- }
Add Comment
Please, Sign In to add comment