I am trying to create a setInterval function that checks to see whether an element exists after a click event. The setInterval works fine but I cant get it to clear when using clearInterval. So what I am trying to achieve is that once the element exists it should stop running the setInterval function. However the function keeps running once the element has loaded on the page. My code is below
editStore()?.addEventListener('click', () => {
const firstInterval = () =>
setInterval(function () {
intervalFunc()
}, 500)
const intervalFunc = () => {
if (newSearchStore()) {
console.log('new search store')
clearInterval(firstInterval())
}
}
firstInterval()
})
What am I doing wrong here? Any help would be really appreciated.
Thanks in advance!