Created
October 14, 2020 06:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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