Adding the answer to James Allardis:
Depending on your implementation and where you are looking for double clicks, you can also check the location of the user's mouse (or, I think, the location of the point). This is to avoid double-clicking when the user clicks things on different parts of your page (again, it depends on the implementation of your event listener - if this is only one button, for example, this is probably not a problem).
When the click event fires an event listener in my example below, there are two variables e.clientX
and e.clientY
. This will give you the location of the mouse. You might want to check if the user has changed since the first mouse click (adapt according to your code).
document.addEventListener("click", function(e){ console.log("Mouse X: " + e.clientX + ": Mouse Y: " + e.clientY); });
You do not want it to be too dense, or the user will never be able to double-click, and you do not want it to be too free, so double clicks seem random to the user. Maybe start with 25px or so around the first click (again, it depends on your application). This is something you can test and customize based on your user interface.
I assume that you do not have jQuery or you are not using it, because I believe that jQuery may already perform this calculation to start dblclick
source share