Quick question; should I use removeChild() or removeEventListener() first?
Also, if I set an object to null, do I still need to remove event listeners from that object?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Whether doing the other way will throw an error or not, totally depends on the event you are listening to & the objects you are trying to access after the event fires.
But it always a good practice to remove event listeners as fast as you can.
For example : If you require the click of a button once & you seem to disable the button after click, It doesn’t make sense to keep the listener running.
And Yes you do need to manually remove all event listeners. But you can also use weak event listeners:
Which hints the garbage collector to remove the listener when object is set to null.