search

Home  >  Q&A  >  body text

Can't remove EventListener in Typescript

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

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.

P粉401901266P粉401901266348 days ago505

reply all(1)I'll reply

  • P粉275883973

    P粉2758839732024-03-31 09:28:36

    Save the event instance into a class field and then call the instance in removeEventListener, for example.

    1

    document.removeEventListener("pointermove", this.savedpointermove);

    It should be fine now

    reply
    0
  • Cancelreply