双击事件怎么解决防止触发单击事件

ref + settimeout

为啥不能使用 state / 静态变量

state 就存在闭包

静态变量无效

handleSingleClick = () => {
    const timer = setTimeout(() => {
      if (!this.isDoubleClick) {
        console.log("Single click");
      }
      this.isDoubleClick = false;
      clearTimeout(timer);
      this.timerRef.current = null;
    }, 200);
    this.timerRef.current = timer;
  }

  handleDoubleClick = () => {
    this.isDoubleClick = true;
    const timer = this.timerRef.current;
    if (timer) {
      clearTimeout(timer);
      this.timerRef.current = null;
    }
    console.log("Double click");
  }
posted @ 2023-08-25 17:53  郭杰前端开发  阅读(25)  评论(0编辑  收藏  举报
## 希望内容对你有帮助,如果有错误请联系我 q: 1911509826,感谢支持