Javascript 从窗口中删除所有侦听器

Javascript 从窗口中删除所有侦听器,javascript,removeeventlistener,Javascript,Removeeventlistener,我想在没有引用的情况下从窗口中删除游戏的和处的侦听器。怎么做?它可以被移除,分离,不管我想做什么 actionListeners = () => { window.addEventListener("keydown", (e) => { if (e.key === "ArrowLeft") { this.isArrowLeft = true; } else if (e.key =

我想在没有引用的情况下从窗口中删除游戏的和处的侦听器。怎么做?它可以被移除,分离,不管我想做什么

 actionListeners = () => {
    window.addEventListener("keydown", (e) => {
        if (e.key === "ArrowLeft") {
            this.isArrowLeft = true;
        } else if (e.key === "ArrowRight") {
            this.isArrowRight = true;
        }
    });

    window.addEventListener("keyup", (e) => {
        if (e.key === " ") {
            this.executeShot();
        }

        if (e.key === "Control") {
            this.executeBarrier();
        }

        if (e.key === "ArrowLeft") {
            this.isArrowLeft = false;
        } else if (e.key === "ArrowRight") {
            this.isArrowRight = false;
        }
    });
};

如果每个事件只需要一个侦听器,则可以使用以下内容:

const actionListeners=()=>{
window.onkeydown=(e)=>{
如果(e.key==“箭头左”){
this.isArrowLeft=true;
}否则,如果(e.key==“ArrowRight”){
this.isArrowRight=true;
}
});
window.onkeyup=(e)=>{
如果(e.key==“”){
this.executeShot();
}
如果(如键==“控制”){
this.executeBarrier();
}
如果(e.key==“箭头左”){
this.isArrowLeft=false;
}否则,如果(e.key==“ArrowRight”){
this.isArrowRight=false;
}
});
};
常量RemovelListeners=()=>{
window.onkeyup=window.onkeydown=null;
}

这是否回答了您的问题@ManasKhandelwal
窗口
不是元素。除非有对侦听器的引用,否则无法从
窗口
文档
中删除事件侦听器。您唯一能做的就是重新加载页面,或使用
document销毁当前文档。在流已关闭时写入
。您的问题是否得到回答?如果是,请选择最佳答案。如果没有,请作出相应评论。