0

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?

2
  • 2
    You are adding event mousemove and removing event mouseup. The events do not match Commented Aug 23, 2018 at 13:28
  • 1
    Also the function is not the same. Commented Aug 23, 2018 at 13:30

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot man for the code. However, what I need to do is when mouseup event fires, mousemove should be removed. Anyway, this is possible?
@Sanjay just call the removeEventListener function inside the event handler function for the mouseup event.
@EmileBergeron Gotcha!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.