I know there are stacks of ways this could
be possible. And I also understand settimeout is async and "true" can't easily be returned, without promises.
But I'm struggling to return true from this function, with promises.
What is the best way I can return true, from this snippet of code? (I want to return true, IF an element exists).
I need to be able to specificially return true
.
Or is there another way I can achieve this. Waiting for a DOM element to exist on a page and return true
when it does?
function waitForElement(className, callBack){
return new Promise(function(resolve) {
window.setTimeout(function(resolve){
var element = document.querySelector(className);
if(element) {
callBack(className, element);
console.log("Element exists")
} else {
waitForElement(className, callBack);
console.log("Wait...")
}
},1000)
});
};
waitForElement(".helping-hand-sliding-data h2",function(){
return true;
});