0

I have an object instance whose one of the methods adds event listeners to a list of month days:

addListenerToDays() {
    monthDays = this.instanceElement.querySelectorAll('.month-days');

    for (let i = 0; i < monthDays.length; i++) {
        monthDays[i].addEventListener ("click", this.setSelection.bind(this), false);
    }

The problem is that I can change the month, then on every month change it binds around 30 new event listeners for every day. So quickly the app slows down until the browser crashes.

How should I properly bind the listeners and remove the old ones?

The above method is called every time a month changes which means I no longer need the old listeners when it's called again.

But how can I remove the specific listeners that were added by this method previously?

pileup
  • 1
  • 2
  • 18
  • 45

0 Answers0