Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent click event from firing when dblclick event fires

I'm handling both the click and dblclick event on a DOM element. Each one carries out a different command, but I find that when double clicking on the element, in addition to firing the double click event, the click event is also fired twice. What is the best approach for preventing this behavior?

like image 651
David Avatar asked May 19 '09 01:05

David


People also ask

How do you stop a single click event when double clicking?

on('click',function(e){ if(e. originalEvent. detail > 1){ return; /* if you are returning a value from this function then return false or cancel the event some other way */ } }); Done.

How do I disable double click event in react?

To prevent multiple button clicks in React: Set an onClick prop on the button, passing it a function. When the button gets clicked, set its disabled attribute to true .

How do you handle double click event in react?

To handle double click events in React:Add an onClick prop to the element. Use the detail property on the event object to get the click count. If the click count is equal to 2, handle the double click event.

Which event type is fired when a user double clicks on an element?

The dblclick event fires when a pointing device button (such as a mouse's primary button) is double-clicked; that is, when it's rapidly clicked twice on a single element within a very short span of time.


1 Answers

In case anyone else stumbles on this (as I did) looking for an answer, the absolute best solution that I could come up with is the following:

    $node.on('click',function(e){       if(e.originalEvent.detail > 1){          return;         /* if you are returning a value from this          function then return false or cancel           the event some other way */       }     }); 

Done. If there is more than one click back to back, the second, third,etc. will not fire. I definitely prefer this to using any sort of timers.

I got myself pointed in this direction by reading this.


Incidentally: I was first researching this problem because I accidentally double clicked a paginated link, and the event fired and finished twice before the callback could happen.

Before coming up with the code above, I had

 if e.originalEvent.detail === 2 //return 

however, I was able to click on the link 3 times (a triple click), and though the second click didn't fire, the third did

like image 89
dgo Avatar answered Sep 24 '22 10:09

dgo