Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从侦听器中删除EventListeners时出现问题_Javascript_Addeventlistener - Fatal编程技术网

Javascript 从侦听器中删除EventListeners时出现问题

Javascript 从侦听器中删除EventListeners时出现问题,javascript,addeventlistener,Javascript,Addeventlistener,在我的JS模块中,我有一个添加了两个事件监听器的函数 clickCatcher.addEventListener("click", this.clickCatcherFunction); window.addEventListener('resize', this.clickCatcherFunction); document.addEventListener('keydown', this.clickCatcherFunction); 调用了clickcatcher函数,到目前为止一切正常。

在我的JS模块中,我有一个添加了两个事件监听器的函数

clickCatcher.addEventListener("click", this.clickCatcherFunction);
window.addEventListener('resize', this.clickCatcherFunction);
document.addEventListener('keydown', this.clickCatcherFunction);
调用了clickcatcher函数,到目前为止一切正常。现在,如果其中一个事件侦听器被触发,则应该删除所有事件侦听器

document.removeEventListener('keydown', this.clickCatcherFunction);
没有错误,但事件侦听器仍处于活动状态。如何访问传入的相同函数?在侦听器函数中,this.someOtherFunc也会失败。这个监听器的作用域是什么,它改变了吗

编辑。为上下文添加更多内容:

export default class Hints {

    constructor(){
        //ref for adding and removing
        this.clickCatcherFunction = this.clickCatcherFunction.bind(this);

        //add invisible top div to catch click and remove hint, 
        const clickCatcher = document.createElement("div"); 
        clickCatcher.id = "clickCatcher";
        document.body.appendChild(clickCatcher);
        clickCatcher.addEventListener('click', this.clickCatcherFunction);
        window.addEventListener('resize', this.clickCatcherFunction);
        document.addEventListener('keydown', this.clickCatcherFunction);
    }

    clickCatcherFunction() {
        //clickCatcher gets deleted from the dom
        document.removeEventListener('keydown', this.clickCatcherFunction);
        window.removeEventListener('resize', this.clickCatcherFunction);
    }
}


//in the JS that imported Hints.JS
import Hints from "../Hints.js"
let hint = new Hints();

将解决方案添加到构造函数中

在添加过程中,每次调用都需要调用
。removeEventListener

因此,从您的代码:

clickCatcher.removeEventListener('click', this.clickCatcherFunction);
window.removeEventListener('resize', this.clickCatcherFunction);
document.removeEventListener('keydown', this.clickCatcherFunction);
记住,我会小心地指出你的方式
将是您正在调用的上下文的封闭对象。这可能不是你想要的

根据您的编辑:

document.addEventListener('keydown',this.clickCatcherFunction.bind(this))


将有帮助。

如果
clickCatcherFunction()
是全局的,它将工作;但只有当所有的添加和删除调用也在全局上下文中发生时,才可以使用
that=this
技巧,或者使用bind()您的回调来获得正确的引用。使用.bind(此)重复(不幸的是,在问题的上一个版本中已经破坏了我的Mjölnir…)通过addEventListener,该函数可以使用this.other引用其他函数。但没有从窗口或文档中删除eventlisteners。但是如果我把this.listener=this.listener.bind(this)放在构造函数中,我可以用它来添加和删除。是的,因为.bind返回一个函数。