Currently, I have a condition in place to verify if I am on a specific URL. If this condition is true, the setInterval()
function will begin checking for a particular element on the webpage.
Once the element is located, a designated function will be executed. If the element is not found, the interval will be stopped after a set period of time:
if(document.location.href.includes('part-of-url')){
var i = 0;
var interval = setInterval(()=>{
if(document.querySelector('.selector')){
someFunction();
}
else{
i++;
}
if(i >= 20){
clearInterval(interval);
}
}, 100)
}
I am facing an issue when it comes to clearing the interval after the function has been called. It seems that based on my understanding of the setInterval()
, the interval continues running even after the function execution. Where should I place the clearInterval() method to ensure it stops after the function is triggered?
The main objective of this code is to execute a function once a specific element is loaded. I attempted to clear the interval after the function call, but it appears that the someFunction()
is never being invoked in that scenario.