Last active
March 9, 2023 22:35
-
-
Save 0187773933/543c9b053f1f5d54f5e0ed37af97194a to your computer and use it in GitHub Desktop.
Vanilla JS ES6 Wait on Element
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
function wait_on_element( query_selector , check_interval=500 , timeout=20000 ) { | |
return new Promise( function( resolve , reject ) { | |
try { | |
let READY_CHECK_INTERVAL = setInterval( function () { | |
let element = document.querySelectorAll( query_selector ); | |
if ( element ) { | |
if ( element[ 0 ] ) { | |
clearInterval( READY_CHECK_INTERVAL ); | |
resolve( element[ 0 ] ); | |
return; | |
} | |
} | |
} , check_interval ); | |
setTimeout( function () { | |
clearInterval( READY_CHECK_INTERVAL ); | |
resolve(); | |
return; | |
} , timeout ); | |
} | |
catch( error ) { console.log( error ); resolve( false ); return; } | |
}); | |
} |
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
function wait_on_element(e,t=500,n=2e4){return new Promise((function(r,o){try{let o=setInterval((function(){let t=document.querySelectorAll(e);if(t&&t[0])return clearInterval(o),void r(t[0])}),t);setTimeout((function(){clearInterval(o),r()}),n)}catch(e){return console.log(e),void r(!1)}}))} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment