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 |
|
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粉2758839732024-03-31 09:28:36
Save the event instance into a class field and then call the instance in removeEventListener, for example.
1 |
|
It should be fine now