Skip to content

Instantly share code, notes, and snippets.

@c-kick
Created October 14, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c-kick/f008b4eaef6acb2fe42a08b3c116efdc to your computer and use it in GitHub Desktop.
Save c-kick/f008b4eaef6acb2fe42a08b3c116efdc to your computer and use it in GitHub Desktop.
This jQuery script enables doubleclick and doubletap (mobile) behaviour, without the need for global variables. It does not rely on jQuery’s ‘dblclick’ handler, but needs to be binded to the regular ‘click’ event.
$(document).on('click', '#MyDoubleClickElement', 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!");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment