繁体   English   中英

node.js删除事件侦听器不起作用

[英]node.js remove event listener not working

我试图删除这样的事件监听器:

    var callback = function () {
      someFun(someobj)
    }

    console.log(callback)

    e.once("5", callback);

    uponSomeOtherStuffHappening('',
    function() {
      console.log(e.listeners("5")[0])
      e.removeListener(inTurns, callback)
    })

但这是行不通的。

第一个控制台日志显示:

[Function]

第二个显示:

[Function: g]

他们为什么不同?

执行一次one()会插入一个函数g()以在一次调用后删除您的侦听器。

从events.js:

EventEmitter.prototype.once = function(type, listener) {
  if ('function' !== typeof listener) {
    throw new Error('.once only takes instances of Function');
  }

  var self = this;
  function g() {
    self.removeListener(type, g);
    listener.apply(this, arguments);
  };

  g.listener = listener;
  self.on(type, g);

  return this;
};

因此,如果您这样做:

console.log(e.listeners("5")[0].listener);

他们会是一样的。

暂无
暂无

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

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