Skip to content

Instantly share code, notes, and snippets.

@dimaspirit
Created February 17, 2018 12:30
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 dimaspirit/f4ad3e3a3cf24396c8d29dad9f950974 to your computer and use it in GitHub Desktop.
Save dimaspirit/f4ad3e3a3cf24396c8d29dad9f950974 to your computer and use it in GitHub Desktop.
Check out and return el when it will be render
/**
* 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