2022-05-30 13:06:34 +00:00
|
|
|
/**
|
|
|
|
* Recursive function that waits up to 3 seconds until an element is found, then calls the callback.
|
|
|
|
*
|
2022-06-21 08:37:34 +00:00
|
|
|
* @param {string} query query of the element.
|
|
|
|
* @param {Function} func callback called when element is found.
|
|
|
|
* @param {number} tries used internally to limit the number of tries.
|
2022-05-30 13:06:34 +00:00
|
|
|
*/
|
|
|
|
export function waitUntilElementIsPresent(
|
|
|
|
query: string,
|
|
|
|
func: () => void,
|
|
|
|
tries = 0
|
|
|
|
) {
|
|
|
|
if ( tries > 6 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout( () => {
|
|
|
|
const element = document.querySelector( query );
|
|
|
|
if ( element ) {
|
|
|
|
func();
|
|
|
|
} else {
|
|
|
|
waitUntilElementIsPresent( query, func, ++tries );
|
|
|
|
}
|
|
|
|
}, 500 );
|
|
|
|
}
|