简体   繁体   English

JavaScript addEventListener

[英]JavaScript addEventListener

When I use addEventListener only once, like in the example file input, should I remove the listener as well with removeEventListener ? 当我只使用addEventListener一次时,就像在示例文件输入中一样,我是否应该使用removeEventListener删除侦听removeEventListener Or, if I know that I will not use any of that code anymore, then garbage collector will collect all the objects anyway? 或者,如果我知道我不再使用任何代码,那么垃圾收集器会收集所有对象吗?

Also, if I remove the event listener manually, will it speed up the garbage collector as it makes its job easier? 另外,如果我手动删除事件监听器,它会加速垃圾收集器,因为它使其工作更容易吗?

  var file    = document.querySelector('input[type=file]').files[0];
  var reader  = new FileReader();

  reader.addEventListener("load", function () {
    preview.src = reader.result;
  }, false);

  if (file) {
    reader.readAsDataURL(file);
  }

should I remove the listener as well with removeEventListener ? 我应该用removeEventListener删除监听removeEventListener吗?

That's entirely up to you. 这完全取决于你。 Unless you do, it will remain attached. 除非你这样做,否则它将保持附加状态。

Or, if I know that I will not use any of that code anymore, then garbage collector will collect all the objects anyway? 或者,如果我知道我不再使用任何代码,那么垃圾收集器会收集所有对象吗?

Only if the FileReader is eligible for garbage collection. 仅当FileReader有资格进行垃圾回收时。 If reader isn't eligible for GC, then its handlers remain in memory. 如果reader不符合GC的条件,则其处理程序将保留在内存中。

Also, if I remove the event listener manually, will it speed up the garbage collector as it makes its job easier? 另外,如果我手动删除事件监听器,它会加速垃圾收集器,因为它使其工作更容易吗?

That will vary by implementation. 这将因实施而异。

Note that with your example, you can't remove that handler. 请注意,使用您的示例,您无法删除该处理程序。 To remove it, you have to have a reference to it. 要删除它,您必须引用它。

Here's an example actually removing it: 这是一个实际删除它的示例:

var file = document.querySelector('input[type=file]').files[0];
var reader;
var handler;
if (file) {
    reader  = new FileReader();
    handler = function () {
        preview.src = reader.result;

        // Remove this handler
        reader.removeEventListener("load", handler, false);
        reader = handler = null;
    };
    reader.addEventListener("load", handler, false);
    reader.readAsDataURL(file);
}

Note that we remove the reference to the handler both from the event handler list and from the handler variable, and we make sure to clear our reader variable as well so the reader is eligible for GC. 请注意,我们从事件处理程序列表 handler变量中删除对处理程序的引用,并且我们确保清除reader变量,以便读者有资格使用GC。

The above may well be overkill, though. 不过,上述情况可能过度。 Just clearing the reader should be sufficient: 只需清理reader

var file = document.querySelector('input[type=file]').files[0];
var reader;
if (file) {
    reader  = new FileReader();
    reader.addEventListener("load", function () {
        preview.src = reader.result;

        reader = null;
    }, false);
    reader.readAsDataURL(file);
}

By release our reference to reader , we make the reader eligible for GC, which will also clean up its event handlers. 通过发布我们对reader的参考,我们使读者有资格获得GC,这也将清理其事件处理程序。

If you really use the listener and (ie the event) just once, it makes sense to remove the listener afterwards. 如果你真的只使用了一个监听器和(即事件),那么之后删除监听器是有意义的。 In particular if you are somehow concerned about performance. 特别是如果你以某种方式关注性能。

The garbage collection will not remove the listener, since it does not know if that event may occur again in the future. 垃圾收集不会删除监听器,因为它不知道将来是否会再次发生该事件。 And because it would not remove the listener by itself, removing it manually does not speed up anything. 并且因为它不会自己删除侦听器,所以手动删除它不会加速任何事情。

Removing it inside the event should be a quite clean solution. 在事件中删除它应该是一个非常干净的解决方案。

  1. It's not mandatory to unbind event handlers to events you don't use in the future but unbinding event manually will make a good solution/code. 将事件处理程序解除绑定到未来未使用的事件并非强制性,但手动解除绑定事件将成为一个好的解决方案/代码。

  2. JavaScript's garbage collection will take care of it and free up the reserved memory. JavaScript的垃圾收集将处理它并释放保留的内存。 It won't remove event listeners to DOM elements that may still have a reference to them in a detached DOM tree. 它不会删除可能仍然在分离的DOM树中引用它们的DOM元素的事件侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM