I'm trying to remove event listeners in typescript. I add event listener in if statement. In the else statement I'm trying to remove these event listeners but for some reason it's not removing them.
FYI: I have a button where I set a boolean value (movePick). If this is true then I want to be able to move my object. This is where the event listener is created. If I click the button again, I can no longer move the object. That's why I tried removing the event listener.
public moveObject(movePick: boolean, raycaster: THREE.Raycaster): void { const plane = new THREE.Plane(); const pNormal = new THREE.Vector3(0, 1, 0); // plane's normal const planeIntersect = new THREE.Vector3(); // point of intersection with the plane const pIntersect = new THREE.Vector3(); // point of intersection with an object (plane's point) const shift = new THREE.Vector3(); // distance between position of an object and points of intersection with the object let isDragging = false; let dragObject: any; // events const pointermove = (event: PointerEvent) => { const rect = this.canvas.getBoundingClientRect(); const x = event.clientX - rect.left const y = event.clientY - rect.top const pickPosition = { x: 0, y: 0 }; pickPosition.x = (x / this.canvas.clientWidth) * 2 - 1; pickPosition.y = (y / this.canvas.clientHeight) * -2 + 1; raycaster.setFromCamera(pickPosition, this.camera); // console.log("movePICK IN POINTERMOVE",movePick) if (isDragging) { raycaster.ray.intersectPlane(plane, planeIntersect); dragObject.position.addVectors(planeIntersect, shift); } } const mousedown = () => { const intersects = raycaster.intersectObjects(this.scene.children); for (let i = 0; i < this.mesharr.length; i++) { if (intersects[0].object.name == this.mesharr[i].name && intersects[0].object.name != "Rail") { pIntersect.copy(intersects[0].point); plane.setFromNormalAndCoplanarPoint(pNormal, pIntersect); shift.subVectors(intersects[0].object.position, intersects[0].point); isDragging = true; dragObject = intersects[0].object; } } } const pointerup = () => { isDragging = false; dragObject = null; } **if (movePick) { document.addEventListener("pointermove", pointermove); document.addEventListener("mousedown", mousedown); document.addEventListener("pointerup", pointerup); } else { document.removeEventListener("pointermove", pointermove); document.removeEventListener("mousedown", mousedown); document.removeEventListener("pointerup", pointerup); }** }
If I delete the event list in the same if statement that I added the event list, they are deleted. But if I click the button again and enter the else statement, I can't delete them. I also tried several solutions in stackoverflow but none of them worked.
Save the event instance into a class field and then call the instance in removeEventListener, for example.
It should be fine now