Doubleclick & tap

Simple jQuery onClick handler that enables double clicking and is mobile (tap) ready

by c_kick

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="doubleclick">
Double click me
</button>

JavaScript

$(document).on('click', '#doubleclick', function () {
  var t = $(this), doubleClickInterval = 500; //set up base vars
  var lastTouch = t.data('lastTouch') || 0, time = new Date().getTime(); //check when this element has been clicked last
  t.data('lastTouch', time); //store this click time
  if (time - lastTouch < doubleClickInterval && lastTouch !== 0) { //check if time between this and previous click exceeds the threshhold. If there is no last click registered, don't handle the callback
    //do your stuff here (execute a callback)
    alert("Double click!");
  }
});