Javascript 单击一定次数后禁用eventListener

Javascript 单击一定次数后禁用eventListener,javascript,event-listener,Javascript,Event Listener,在收到一定数量的单击后,如何禁用事件侦听器 我认为下面的代码是错误的,因为我要求从事件侦听器中禁用事件侦听器 let cells=document.querySelectorAll('.cell'); 设温度=0; 功能点击器(x){ 温度+=1; console.log('clicked',x.target.classList[1],temp); x、 target.style.backgroundColor='red'; 如果(温度==3){ cells.forEach(c=>c.remo

在收到一定数量的单击后,如何禁用事件侦听器

我认为下面的代码是错误的,因为我要求从事件侦听器中禁用事件侦听器

let cells=document.querySelectorAll('.cell');
设温度=0;
功能点击器(x){
温度+=1;
console.log('clicked',x.target.classList[1],temp);
x、 target.style.backgroundColor='red';
如果(温度==3){
cells.forEach(c=>c.removeEventListener('click',(el)=>clicker(el));
}
返回;
}
cells.forEach(c=>c.addEventListener('click',(el)=>clicker(el))
.main{
显示:网格;
网格模板列:重复(3,90px);
网格模板行:重复(290px);
行间距:5px;
柱间距:5px;
}
.细胞{
宽度:90px;
高度:90px;
背景颜色:蓝色;
}

传递给
removeEventListener
的函数必须与传递给
addEventListener
的函数相同。由于您在每个位置都使用匿名函数,因此它们不会是相同的函数

只需使用函数名,而不是调用它的匿名函数

let cells=document.querySelectorAll('.cell');
设温度=0;
功能点击器(x){
温度+=1;
日志('clicked',temp);
如果(温度==3){
cells.forEach(c=>c.removeEventListener('click',clicker));
}
返回;
}
cells.forEach(c=>c.addEventListener('click',clicker))
.main{
显示:网格;
网格模板列:重复(3,90px);
网格模板行:重复(290px);
行间距:5px;
柱间距:5px;
}
.细胞{
宽度:90px;
高度:90px;
背景颜色:蓝色;
}