that's a good catch, I didn't even notice that!
This will indefinitely loop and be problematic for sure. Good idea to recommend setInterval. However, I am struggling to see why there should be a loop or interval in the first place. They are adding an event listener to some items & those will stay... No need to reapply. If the event listeners are being removed somewhere, they should be reapplied on an action, not on an interval. I may be wrong, but I think that code will work fine without while(true) - duh - AND also without an interval.
BUT while we're on the topic, I mind as well make some recommendations about the interval functions for JS.
You can also use setTimeout if you want to cancel the loop at some point like this...
JavaScript:
var IntervalLoop = setInterval(function(){
// do stuff here
}, 10000); // do it every 10 seconds
setTimeout(function(){
clearInterval(IntervalLoop);
}, 100000); // after 100 seconds, run this...
In that example you have set up a variable (intervalLoop) to represent the code that runs every 10 seconds.
We do this so that you can also use setTimeout() to STOP the code from running after 100 seconds.
You could do something like this too... This would only stop the code if userActive = false, so you could use different code to track if the user is still on the page or not.
This prevents scripts from running non stop even if the user is not there. For example, this can come in handy with chatroom or forum thread auto-refresh/load scripts to prevent database requests when a user leaves a page open on their screen but isn't actually at the computer. Of course, you would need to come up with the logic to decide if/when userActive should be true/false.
JavaScript:
setTimeout(function(){
if(userActive === false){
clearInterval(IntervalLoop);
}
}, 100000); // after 100 seconds, check if user is still here