简体   繁体   English

可以通过取消其回调函数来删除EventListener吗?

[英]Can an EventListener be removed by nullifying its callback function?

I was wondering if an event listener can be removed by nullifying its callback function? 我想知道是否可以通过取消其回调函数来删除事件监听器?

Simplified Example: 简化示例:

var somefunction = function() {
  // some code
}
window.addEventListener(eventType, somefunction, false); 

Now, will setting somefunction = null; 现在,将设置somefunction = null; remove the above EventListener, or will it simply turn it into a zombie EventListener? 删除上面的EventListener,还是将其简单地变成僵尸EventListener?

The actual code is used in a Firefox (overlay-type) Addon and I was contemplating alternative methods of (automatically) removing EventListeners on unload event, other than the obvious: 实际的代码在Firefox(覆盖类型)插件中使用,我正在考虑(显而易见的)在unload事件上(自动)删除EventListener的替代方法,除了显而易见的方法:

window.removeEventListener(eventType, somefunction, false); 

Update: Please note that this is part of a code for a Firefox addon. 更新:请注意,这是Firefox插件代码的一部分。 The eventType in this instance is 'popupshowing' which can not be nullified as it would disrupt browser functions. 在这种情况下,eventType是'popupshowing' ,不能将其无效,因为它将破坏浏览器的功能。

Thank you in advance for your help Looking forward to (alternative) suggestions 预先感谢您的帮助期待(替代)建议

removeEventListener is the way to go. removeEventListener是必经之路。

Also, you don't actually nullify the function by setting some variable to null . 同样,您实际上并不通过将某些变量设置为null使函数null The variable has assigned just a reference (to a non-POD object, like a function). 该变量仅分配了一个引用(对非POD对象,如函数)。 To illustrate this, consider the following: 为了说明这一点,请考虑以下内容:

var a = function() { alert("called"); };
setTimeout(a, 1000); // Will still alert!

var b = a;

a = null; // What you called `nullify`....
console.log(this.a, "a" in this); // null, true
delete this.a; // Actually remove the property from the global scope.
// `a` is really dead at this point!

b(); // will still alert, too.

If you want to avoid some removeEventListener calls, I'd use some helper function(s): 如果要避免一些removeEventListener调用,我将使用一些辅助函数:

let { addEventListenerUnload, removeEventListenerUnload } = (function() {
  let tracked = [];
  addEventListener("unload", function removeTracked() {
    removeEventListener("unload", removeTracked);
    for (let t of tracked) {
      try {
        removeEventListener(t.type, t.fn, t.capture);
      }
      catch (ex) {}
    }
    tracked.length = 0;
  });
  return {
    addEventListenerUnload: function(type, fn, capture) {
      addEventListener(type, fn, capture);
      tracked.push({type: type, fn: fn, capture: capture});
    },
    removeEventListenerUnload: function(type, fn, capture) {
      tracked = tracked.filter(e => e.type != type || e.fn != fn || e.capture != capture);
      removeEventListener(type, fn, capture);
    }
  };
})();

(Includes some ECMA-6 stuff that Firefox supports, but which you could easily convert. Also, removeEventListenerUnload might not be needed at all, so you might omit it. Also, when using this in overlay script make sure to give it unique names to avoid clashes with other code). (包括Firefox支持的ECMA-6内容,但您可以轻松转换。此外,可能根本不需要removeEventListenerUnload ,因此您可以省略它。此外,在覆盖脚本中使用此名称时,请确保为其指定唯一的名称避免与其他代码冲突)。

我不认为将回调函数设置为null会删除eventlistener,您仍然会附加eventlistener,可以使用removeEventListener或将eventType设置为null,例如:

window.eventType = null;

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

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