jackmarketon
5/29/2018 - 3:48 PM

Wait for element to exist

// Utility wait functionality
function doesElementExist(identifer, callback) {
	const MAX_INTERVAL = 60000; // 1 Minute
	const INTERVAL = 500; // .5 second
	let intervalCount = 0;
	const ticker = window.setInterval(() => {
  	if (intervalCount * INTERVAL >= MAX_INTERVAL) {
    	callback(undefined); // error state
      window.clearTimeout(ticker);
    } else if (document.querySelector(identifer)) {
    	callback(document.querySelector(identifer));
      window.clearTimeout(ticker);
    }
  }, INTERVAL);
}