Skip to content

Instantly share code, notes, and snippets.

@tsmd
Created July 14, 2021 05:12
Show Gist options
  • Save tsmd/e5bba7dd90c18730abac23258a5eb26c to your computer and use it in GitHub Desktop.
Save tsmd/e5bba7dd90c18730abac23258a5eb26c to your computer and use it in GitHub Desktop.
export function waitForGlobalVariable(objPath, interval, timeout, callback) {
function find(objPath) {
var found = window;
objPath = objPath.split(".");
while (objPath.length > 0) {
var objPart = objPath.shift();
if (objPart in found) {
found = found[objPart];
} else {
return null;
}
}
return found;
}
function findAndCallback(objPath, callback) {
var found = find(objPath);
if (found) {
callback();
return true;
}
return false;
}
var timer = 0;
timer = setInterval(function () {
var found = findAndCallback(objPath, callback);
if (found) {
clearInterval(timer);
}
}, interval);
setTimeout(function () {
clearInterval(timer);
}, timeout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment