Created
February 17, 2018 12:30
-
-
Save dimaspirit/f4ad3e3a3cf24396c8d29dad9f950974 to your computer and use it in GitHub Desktop.
Check out and return el when it will be render
This file contains 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
/** | |
* Check out and return el when it will be render | |
* @param {string} query - string containing CSS selector | |
* | |
* @example | |
* const mountElClassName = '.mountEl'; | |
* | |
* isRenderedDOMElement(mountElClassName).then((el) => { | |
* el.append('Your included html'); | |
* }).catch(() => { | |
* throw new Error('Element not found'); | |
* }); | |
*/ | |
function isRenderedDOMElement(query) { | |
return new Promise( (resolve, reject) => { | |
const timeToWaiting = 10; // sec | |
let counter = 0; | |
let timerId = setInterval(() => { | |
let el = document.querySelector(query); | |
if(el) { | |
clearInterval(timerId); | |
resolve(el); | |
} else if(counter > timeToWaiting) { | |
clearInterval(timerId); | |
reject(); | |
} else { | |
++counter; | |
} | |
}, 1000); | |
}); | |
}; | |
export { isRenderedDOMElement }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment