I can't remove event when action is end. I init event by click:
<span class="leftTopPoint" (click)="initResize($event)"></span>
export class SectionComponent {
...
initResize(e): void {
this.mouseX = e.clientX;
this.mouseY = e.clientY;
document.addEventListener('mousemove', this.onResize.bind(this), false);
document.addEventListener('mouseup', this.stopResize.bind(this), false);
}
}
I used .bind(this) and then pointer this is okay, but when I call to method of stopResize(), removeEventListener doesn't work. Method onResize() still work.
export class SectionComponent { ...
stopResize(e): void {
document.removeEventListener('mousemove', this.onResize, false);
document.removeEventListener('mouseup', this.stopResize, false);
}
}
To resolve the removeEventListener not working issue, make sure to pass the same function to the addEventListener and removeEventListener methods. Passing a function that points to a different location in memory will not remove the listener.
The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.
addeventListener is not a function", which means that the function we're calling is not recognized by the JavaScript interpreter. Often, this error message actually means that we've spelled something wrong. If you are not sure of the correct spelling of a piece of syntax, it is often good to look up the feature on MDN.
Event listeners can also be removed by passing an AbortSignal to an addEventListener() and then later calling abort() on the controller owning the signal.
Here is an elegant way with little boilerplate by using the fat arrow function:
export class AppComponent {
private classMember = 'aClassMember';
// use the fat arrow to bind the event
private eventListener = () => {
console.log(this.classMember);
};
registerEvent() {
this.htmlEl.addEventListener('click', this.eventListener);
);
unregisterEvent() {
this.htmlEl.removeEventListener('click', this.eventListener);
);
}
hints:
You have to specify the same function to removeEventListener
as you provided to addEventListener
. The function returned by bind
is not the same as the original function (if it were, it would have the this
issue).
So you'll have to store your bound functions and use them when calling removeEventListener
.
initResize(e): void {
this.mouseX = e.clientX;
this.mouseY = e.clientY;
if (!this.onResizeBound) {
this.onResizeBound = this.onResize.bind(this);
}
if (!this.stopResizeBound) {
this.stopResizeBound = this.stopResize.bind(this);
}
document.addEventListener('mousemove', this.onResizeBound, false);
document.addEventListener('mouseup', this.stopResizeBound, false);
}
and
stopResize(e): void {
document.removeEventListener('mousemove', this.onResizeBound, false);
document.removeEventListener('mouseup', this.stopResizeBound, false);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With