I want to call a function on single click and double click on an video element in react.But when i double click the element it also calling the single click function
I want the solution of the problem
Here is the video element
<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>
2
Answers
You can add this mechanism to your code to handle a single click. There is no default handler to prevent a single click in this scenario, according to my knowledge.
We have a term in reactive programming, called debouncing. Debouncing limits the occurrence of an event to a defined interval.
For example, a click event can happen once (only) in 2 seconds. In other words, the user cannot run the click event more than 1 time in 2 seconds. In other words, the user cannot send repetitive click events. In other words, the user should wait at least 2 seconds to execute a click event again. In other words, debouncing bounces events.
I just provided a solution with setTimeOut function, but I highly recommend you look at the RxJS library.