Skip to content Skip to sidebar Skip to footer

Does Remove A Dom Object (in Javascript) Will Cause Memory Leak If It Has Event Attached?

So, if in the javascript, I create a DOM object in the HTML page, and attach event listener to the DOM object, upon I remove the the DOM from HTML page, does the event listener sti

Solution 1:

The sad thing is, the W3C does not have an events collection where you can sift through all events applied to a single element. You could do it manually (i.e. obj.Events = {}; obj.Events[type] = []; obj.Events[type].push(fn) for each event that is added. Event[types] is an array so if you have multiple functions you wish to fire at a time, you can remove each individually), then loop through the obj.Events object to remove all events before removing the object.

Solution 2:

JavaScript does garbage collection automatically for you.

It might free it right away or it might wait when the moment is best. This is up to the JavaScript implementation.

So no, it won't cause a memory leak.

Post a Comment for "Does Remove A Dom Object (in Javascript) Will Cause Memory Leak If It Has Event Attached?"