Reputation: 559
I have a script which waits for specific DOM-Elements to be loaded and when loaded it appends another HTML-Element:
var checkExistVOID = setInterval(function() {
if( $('#voidDocumentsAwaitingMyApproval').length && $('#voidDocumentsAwaitingMySignature').length ) {
$("#loadingBarPolarion1").hide();
$('<span style="font-size: 14px;"><i style="color: lightgreen; padding-right: 10px;" class="fas fa-check"></i>Currently no Document Actions required</span>').appendTo('#voidDocumentsAwaitingMyApproval');
clearInterval(checkExistVOID);
}
}, 500);
My aim was to only trigger this function once, that's why I used the Method:
clearInterval()
It seems to work fine at first glance, but if the site where the page is executed stays opened for about ~30 minutes without any user action, the append
-function is all of a sudden executed every 500 ms
on a loop:
I was wondering, what can be the issue of this? Is it possible that clearInterval()
suddenly isn't "stored anymore"?
Can you suggest me what to change in my function in order to not run into this problem?
I was thinking about an additional check for the already exiting <span>
-ELement and use that in the if
-Function, but I guess that's not a professional solution :)
Upvotes: 0
Views: 33
Reputation: 97382
You're conditionally clearing the interval. If the condition is not met, the interval will not be cleared.
My aim was to only trigger this function once
You should use setTimeout()
instead.
Upvotes: 2