I stumbled on this piece of code while reading about JavaScript events, and I don't understand why the callback function for removeEventListener doesn't lead to the function being called over and over again. Here's the code:
const button = document.querySelector('button');
function once() {
alert('Done');
button.removeEventListener('click', once);
}
button.addEventListener('click', once);
When is clicked, the function onceis called, alerting and then calling removeEventListener - but then removeEventListener calls once, so in my mind this should result in alerts being fired indefinitely. When I load the page however, it works out. Why so?