Skip to content

Instantly share code, notes, and snippets.

@tlync
Created June 9, 2011 03:45
Show Gist options
  • Save tlync/1016011 to your computer and use it in GitHub Desktop.
Save tlync/1016011 to your computer and use it in GitHub Desktop.
jquery double tap plugin - Bind an event handler to the "double tap" JavaScript event.
(function($){
var hasTouch = /android|iphone|ipad/i.test(navigator.userAgent.toLowerCase()),
eventName = hasTouch ? 'touchend' : 'click';
/**
* Bind an event handler to the "double tap" JavaScript event.
* @param {function} doubleTapHandler
* @param {number} [delay=300]
*/
$.fn.doubletap = function(doubleTapHandler, delay){
delay = (delay == null) ? 300 : delay;
this.bind(eventName, function(event){
var now = new Date().getTime();
// the first time this will make delta a negative number
var lastTouch = $(this).data('lastTouch') || now + 1;
var delta = now - lastTouch;
if(delta < delay && 0 < delta){
// After we detct a doubletap, start over
$(this).data('lastTouch', null);
if(doubleTapHandler !== null && typeof doubleTapHandler === 'function'){
doubleTapHandler(event);
}
}else{
$(this).data('lastTouch', now);
}
});
};
})(jQuery);
@ASoodan
Copy link

ASoodan commented Dec 10, 2013

How to use this library, Please attach the sample code

@Paul-Isache
Copy link

$('.yourdiv').doubletap() ---> how you use it

@yairEO
Copy link

yairEO commented Feb 17, 2014

to check for "touch" support just do: var hasTouch = 'ontouchend' in document

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment