如何防止双击react时单击

问题描述 投票:0回答:1

我想在反应中单击并双击视频元素来调用函数。但是当我双击该元素时,它也调用单击函数

我想要问题的解决方案

这是视频元素

<video
                      id={`post${index}`}
                      width="640"
                      height="360"
                      // controls
                      // onDoubleClick={() =>
                      //   setdoublefunction(item.post_id, index)
                      // }
                      onDoubleClick={(event)=>{
                        handleDoubleClick (event,index)
                      }}
                      className="post__media_video"
                      alt="Post Content"
                      style={{
                        objectFit: "contain",
                        background: "black",
                      }}
                      onClick={(event) => {
                          videoFunction(event,index)
                     
                      }}
                    >
                      <source src={item.image} type="video/mp4" />
                      <source src={item.image} type="video/webm" />
                      Your browser does not support the video tag.
                    </video>
javascript reactjs next.js react-hooks onclick
1个回答
0
投票
const [clickCount, setClickCount] = useState(0);

  const handleClick = () => {
    setClickCount(prevCount => prevCount + 1);
    setTimeout(() => {
      if (clickCount === 1) {
        // Single click action
        console.log('Single click action');
      }
      setClickCount(0);
    }, 250); // Change the delay as needed for distinguishing between single and double clicks
  };

您可以将此机制添加到您的代码中以处理单击。据我所知,在这种情况下没有默认处理程序可以防止单击。

© www.soinside.com 2019 - 2024. All rights reserved.