The purpose of this code was to create the click action and double click executor,
but it actually works very poorly ... It's not very good at all, so I would like to know the best way to do this.
Thus, I completely lost the sensitivity of one click, and now it only lingers,
as well as a double click is not very close.
var DELAY = 150, clicks = 0, timer = null; $(function(){ $('button').on("click", function(e){ clicks++; if(clicks === 1) { timer = setTimeout(function() { $('button').html("Single Click"); clicks = 0; }, DELAY); } else { clearTimeout(timer); $('button').html("Double Click"); clicks = 0; } }) .on("dblclick", function(e){ e.preventDefault(); }); });
button { padding: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button>click Me</button>
source share