I found that John Strickler's answer didn’t quite do what I expected. As soon as a warning is triggered by a second click in a two-second window, each subsequent click triggers another warning until you wait two seconds before clicking again. So with John's code, a triple click acts like two double clicks, where I expect it to act like a double click, followed by a single click.
I reworked his decision to function in this way and flow in such a way that the mind could better understand. I delayed the delay from 2000 to 700 to better mimic what I would feel as normal sensitivity. Here's the fiddle: http://jsfiddle.net/KpCwN/4/ .
Thanks for the creation, John. I hope this alternate version is useful to others.
var DELAY = 700, clicks = 0, timer = null; $(function(){ $("a").on("click", function(e){ clicks++; //count clicks if(clicks === 1) { timer = setTimeout(function() { alert("Single Click"); //perform single-click action clicks = 0; //after action performed, reset counter }, DELAY); } else { clearTimeout(timer); //prevent single-click action alert("Double Click"); //perform double-click action clicks = 0; //after action performed, reset counter } }) .on("dblclick", function(e){ e.preventDefault(); //cancel system double-click event }); });
Garland Pope Oct 21 '11 at 5:22 2011-10-21 05:22
source share