JSFiddle - React, Tailwind, and code Playground
by grammar
HTML
<div id="output">Can't find tmgAds...</div>
JavaScript
var output = document.getElementById('output'),
intervalCounter = 0,
maxCount = 50,
i = setInterval(function() {
if(intervalCounter <= maxCount) {
console.log("inside interval");
if(doesTmgAdsExist()) {
console.log("found it... exiting interval");
clearInterval(i);
insertPreviews();
}
intervalCounter++;
} else {
clearInterval(i);
output.innerHTML = "Could not find tmgAds after " + maxCount + " attempts... Giving up hope.";
}
}, 100);
function doesTmgAdsExist() {
return typeof tmgAds !== "undefined";
}
function insertPreviews() {
console.log("insert previews here...");
document.getElementById('output').innerHTML = "Found tmgAds!";
}
// Change this variable's value to 10000 to see that the polling will stop
// after `maxCount` attempts
var timeoutAmount = 1000;
setTimeout(function() {
window.tmgAds = {};
}, timeoutAmount);