JavaScript 使用clearInterval后仍然会触发setInterval里的一些代码


let F=true;
  let gamelisten = this.setInterval(() => {
    document.onkeydown = (e) => {
      //检测移动
      if (F) {
          F=false;
        setTimeout(() =>{console.log(321); ;F=true;},200);  
        let KeyNum = e.which || e.keyCode;
        if (KeyNum == 87) {
          if (BoxArr[player.X][player.Y - 1].isAcompany == 1) {
            console.log(123);
            clearInterval(gamelisten);
          }
          if (BoxArr[player.X][player.Y - 1]._collision == 0) {
            player.Y -= 1;
          }
        }
//后面结构一样就不全复制上来了
      }
    };
    player.m_position();
  }, 160);
};

写的一个检测碰撞的,打印321后控制的单位确实动不了了,但是键盘继续输出后依然会打印123,如果往碰撞的方向移动也会打印321;
也就是说我把定时器关了之后this.document.onkeydown似乎还在发挥作用,但是如果还在发挥作用为什么控制的单位又移动不了了

this.document.onkeydown只要安装就会成效的,不用重复定时设置。除非你重新设置为null。