Skip to content

Instantly share code, notes, and snippets.

@SparK-Cruz
Created January 12, 2018 18:42
Show Gist options
  • Save SparK-Cruz/e99665d2c38916fe70734e0d99346643 to your computer and use it in GitHub Desktop.
Save SparK-Cruz/e99665d2c38916fe70734e0d99346643 to your computer and use it in GitHub Desktop.
export const waitFor = function(search :function, callback :function, interval = 500) :void {
const checkLoop = setInterval(function () {
let element = search();
if (typeof element === 'undefined' || element === null) {
return;
}
clearInterval(checkLoop);
callback(element);
}, interval);
};
export const waitForElement = function(selector :string, callback :function, interval = 500) :void {
waitFor(function(){ return document.querySelector(selector); }, callback, interval);
};
export const waitForElements = function(selector :string, callback :function, interval = 500) :void {
waitFor(function(){ return document.querySelectorAll(selector); }, callback, interval);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment