Javascript removeEventListener不工作?

Javascript removeEventListener不工作?,javascript,addeventlistener,event-listener,Javascript,Addeventlistener,Event Listener,以前有人问过好几次,但我看到和尝试过的所有一个都不是,或者因为某种原因似乎不起作用 Onener接受一个回调,每当按下enter键时它都会触发(这很有效),但当我尝试调用removeEventListener()时,它似乎不起作用。我尝试将函数设置为变量而不是声明,尝试为添加/删除设置useCapture标志,尝试将.bind(this)绑定到函数参数或函数本身,并将removeEventListener()行位于不同的位置(在设置超时()之前/之后),但没有任何效果。事件侦听器要么持续存在(并

以前有人问过好几次,但我看到和尝试过的所有一个都不是,或者因为某种原因似乎不起作用

Onener接受一个回调,每当按下enter键时它都会触发(这很有效),但当我尝试调用
removeEventListener()
时,它似乎不起作用。我尝试将函数设置为变量而不是声明,尝试为添加/删除设置
useCapture
标志,尝试将
.bind(this)
绑定到函数参数或函数本身,并将
removeEventListener()
行位于不同的位置(在
设置超时()之前/之后),但没有任何效果。事件侦听器要么持续存在(并在
div
上累积),要么在某些尝试中根本没有添加

MyConsole.prototype.onEnter = function(callback) {
    const callCallback = function(e) {
        e.preventDefault();
        if (e.keyCode === 13 && typeof callback === "function") {
            setTimeout(function () {
                callback();
            }.bind(this), 0);
            this.textAreaInputDiv.removeEventListener("keyup", callCallback.bind(this), true);
        }
    }
    this.textAreaInputDiv.addEventListener("keyup", callCallback.bind(this), true);
}

任何帮助都将不胜感激

您应该将完全相同的函数传递给
addEventListener
removeEventListener

MyConsole.prototype.onEnter = function(callback) {
    const callCallback = function(e) {
        e.preventDefault();
        if (e.keyCode === 13 && typeof callback === "function") {
            setTimeout(function () {
                callback();
            }.bind(this), 0);
            this.textAreaInputDiv.removeEventListener("keyup", callCallbackBound, true);
        }
    };

    const callCallbackBound = callCallback.bind(this);

    this.textAreaInputDiv.addEventListener("keyup", callCallbackBound, true);
};
事实上,从那时起,a将是一个更好的选择

您可能是指
setTimeout
中的
callback.bind(这个)
,所以我也让自己来解决这个问题:

MyConsole.prototype.onEnter = function(callback) {
    const callCallback = (e) => {
        e.preventDefault();
        if (e.keyCode === 13 && typeof callback === "function") {
            setTimeout(callback.bind(this), 0);
            this.textAreaInputDiv.removeEventListener("keyup", callCallback, true);
        }
    };

    this.textAreaInputDiv.addEventListener("keyup", callCallback, true);
};

您应该将完全相同的函数传递给
addEventListener
removeEventListener

MyConsole.prototype.onEnter = function(callback) {
    const callCallback = function(e) {
        e.preventDefault();
        if (e.keyCode === 13 && typeof callback === "function") {
            setTimeout(function () {
                callback();
            }.bind(this), 0);
            this.textAreaInputDiv.removeEventListener("keyup", callCallbackBound, true);
        }
    };

    const callCallbackBound = callCallback.bind(this);

    this.textAreaInputDiv.addEventListener("keyup", callCallbackBound, true);
};
事实上,从那时起,a将是一个更好的选择

您可能是指
setTimeout
中的
callback.bind(这个)
,所以我也让自己来解决这个问题:

MyConsole.prototype.onEnter = function(callback) {
    const callCallback = (e) => {
        e.preventDefault();
        if (e.keyCode === 13 && typeof callback === "function") {
            setTimeout(callback.bind(this), 0);
            this.textAreaInputDiv.removeEventListener("keyup", callCallback, true);
        }
    };

    this.textAreaInputDiv.addEventListener("keyup", callCallback, true);
};

你为什么要为keyevent安排这么多听众?对一个事件使用一个eventListener,并多次将其委托给DOM中的任何对象。附加到文档对KeyCode13很有好处。为什么keyevent会有这么多侦听器?对一个事件使用一个eventListener,并多次将其委托给DOM中的任何对象。附加到文档适用于keyCode13