Axel
Axel

Reputation: 5111

removeEventListener doesn't work in Javascript

I have the following code to listen and remove the event. However, the event doesn't get removed:

window.addEventListener('mousemove', (event) => {
    this.controlColumnWidth(event, startOffset, column)
})
window.removeEventListener('mouseup', this.controlColumnWidth)

How do I fix this?

Upvotes: 0

Views: 62

Answers (1)

Decade Moon
Decade Moon

Reputation: 34286

You're mixing two different events here, mousemove and mouseup.

Also you need to make sure you remove the same (event) => { ... } function instance that was originally registered:

const handler = event => {
  this.controlColumnWidth(event, startOffset, column);
};

window.addEventListener('mousemove', handler);
window.removeEventListener('mousemove', handler);

// You can also store the handler on `this` if you need to remove
// the event in a different function (such as in the destroyed hook)

Upvotes: 3

Related Questions