I have a function which is looking for a specific element in an interval:
function waitForElementToDisplay(selector, callback, checkFrequencyInMs, timeoutInMs) {
var startTimeInMs = Date.now();
(function loopSearch() {
if (document.querySelector(selector) != null) {
callback();
return;
} else {
setTimeout(function() {
if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
return;
loopSearch();
}, checkFrequencyInMs);
}
})();
}
I use this function in another function which should return gameCode attribute from an element:
function getGameCode() {
return waitForElementToDisplay(".gamesinfo-popup__button",
function() {
return document.querySelector(".gamesinfo-popup__button").getAttribute('data-game-code')
}, 1000, 9000);
}
However when I call getGameCode()
function I get undefined. Calling each parts one by one works as expected so I am guessing that the problem is that I am not passing the returned value inside getGameCode()
function properly. What is the correct way of doing this?