44

I'd like to detect if a user has double tapped on an ipad or iphone.

Does a javascript-event exist which would provide this functionality?

Alex W
  • 33,401
  • 9
  • 92
  • 97
enne87
  • 1,963
  • 8
  • 29
  • 55

11 Answers11

38

This could be used for a double tap or a double click. In pure javascript:

var mylatesttap;
function doubletap() {

   var now = new Date().getTime();
   var timesince = now - mylatesttap;
   if((timesince < 600) && (timesince > 0)){

    // double tap   

   }else{
            // too much time to be a doubletap
         }

   mylatesttap = new Date().getTime();

}
Jorge
  • 801
  • 12
  • 17
  • 2
    interesting solution. I wonder which is more efficient, the set/clear timeout or this. – Vlad Nicula Apr 22 '13 at 07:51
  • this code does not detect single tap! it only detects double tap – Behrouz.M Mar 14 '15 at 15:51
  • 4
    @raypixar: this is exactly what Jorge said and what the asker wanted – Thariama Jul 10 '15 at 12:36
  • 2
    @Thariama Double tap detection without Single tap detection has no meaning. Both of them should be detected. – Behrouz.M Jul 11 '15 at 10:05
  • 3
    quick suggestion: add `if (mylatesttap === undefined) { mylatesttap = new Date().getTime(); }` at the beginning of the function. You also might want to throw in a `setTimeout(singleTap, doubleTapTimeout + 10)` where doubleTapTimeout would be the timeout in ms (in the example in answer this would be `600`) and `singleTap` is a function that checks if the double tap was implemented (this would require another "global" variable under the window variable (such that if the double tap is successful -- where you have the `// double tap` comment -- it would have `true` stored into it). cheers & thanks – dylnmc Feb 03 '17 at 17:19
  • 3
    @raypixar: basically the 'else' part of the condition is the single tap – EdC Jan 12 '18 at 06:18
33

One basic idea is to do it like this:

  1. In order to create a double-tap (or double click) event you need to create code on the onClick event.

  2. The reason you most likely want double-tap/click is because you already have something attached to the onClick event and need a different gesture on the same element.

  3. This means that your onClick event should only launch the onClick event after a setTimeout() is acknowledged.

  4. So the basic code would launch the function attached to the onClick event using a setTimeout() command. The first click says "Start timer + run function using setTimeout() after say..500 milliseconds. The second time you clicked, you would check to see if the second click was inside a specific time frame in order to count as a double tap. So if the End time was less than 500 milliseconds, you would cancel the setTimeout() using clearTimeout() and then launch a completely different function (the function you wanted to launch for double tab/click)

  5. Stopping default action? - Probably somthing like stopPropagation() or cancelBubble() would do the trick. Honestly, I don't know but that's where I'd start researching.

Andy Hayden
  • 291,328
  • 80
  • 565
  • 500
Adam
  • 347
  • 3
  • 4
23

Detect Double Tap

try the following snippet on a touch device

event.preventDefault() will disable double tap zoom on div#double-tap

document.getElementById("double-tap").addEventListener("touchstart", tapHandler);

var tapedTwice = false;

function tapHandler(event) {
    if(!tapedTwice) {
        tapedTwice = true;
        setTimeout( function() { tapedTwice = false; }, 300 );
        return false;
    }
    event.preventDefault();
    //action on double tap goes below
    alert('You tapped me Twice !!!');
 }
   
div#double-tap {
  height: 50px;
  width: 200px;
  border: 1px solid black;
  background-color: lightblue;
  line-height: 50px;
  text-align: center;
  margin: 50px auto;  
}
<div id="double-tap">Double Tap Me !!!</div> 
Anulal S
  • 5,824
  • 5
  • 21
  • 31
  • 1
    Maybe you need add event1.pageX - event0.pageX < xx to avoid two fingers touch trigger double tap. – gonjay Nov 01 '19 at 02:25
19

I created this one in JavaScript and it seems to work well. (I tested it on my iPad 2.) It is basically the code for what is said above.

var clickTimer = null;

function touchStart() {
    if (clickTimer == null) {
        clickTimer = setTimeout(function () {
            clickTimer = null;
            alert("single");

        }, 500)
    } else {
        clearTimeout(clickTimer);
        clickTimer = null;
        alert("double");

    }
}
Dr. Aaron Dishno
  • 1,609
  • 1
  • 25
  • 23
9

I found this solution on Stackoverflow somewhere but forgot what exact post. It works for a doubleclick on a browser and double tap on iPhone.

I use 'touchend click' to detect the double click on both browser and iPhone. To implement just on iPhone, remove the click.

var timeout;
var lastTap = 0;

$('element').on('touchend click',function(e){
    var currentTime = new Date().getTime();
    var tapLength = currentTime - lastTap;        

    e.preventDefault();
    clearTimeout(timeout);

    if(tapLength < 500 && tapLength > 0){

        //Double Tap/Click

    }else{

        //Single Tap/Click

        timeout = setTimeout(function(){
            //Single Tap/Click code here

            clearTimeout(timeout); 
        }, 500);
    }
    lastTap = currentTime;
});
lyubeto
  • 125
  • 3
  • 10
Abdullah
  • 91
  • 1
  • 2
7

Double tap on an iPad is tricky because the browser, by default, absorbs this event as a "zoom" event, as seen here:

Doubletap - Apple API docs

However, if you cancel the default behavior, you can grab the doubletap yourself. Here's an example of a plugin that does this for you-- the jQuery doubletap plugin.

Interrobang
  • 15,663
  • 3
  • 50
  • 61
  • that plugin works very bad with multitouch gestures on the selector – Francesco Apr 27 '12 at 01:33
  • 1
    Use `event.preventDefault();` to disable the default zoom event. I recommend doing this within the specific element you want to double click on rather than the whole document. – Amicable Sep 06 '13 at 14:42
2

Might be late (Actually yup it's damn late), but the pure and easy JS solution for those lurking in the future:

var tapped="";
function dbtapper(elem=false,arg1, arg2, arg3, timer=1400){
    if(tapped==""){
        //Here is single tap

        tapped = setTimeout(function(){tapclean()}, timer)
        lastclick = elem;
        //variable lastclick is to prevent double clicking from happen
        //when clicking different object with double-click listener.

    } else if(lastclick==elem){
        //Here is double tap, if you want more (triple&beyond),
        //add doubletap/tripletap checker variable into tapclean().
        //and set it to true here!, example included.
        //you could also add more setTimeout to var-tapped so user will
        //have more time to do triple&up taps.

        tapclean();
        //doubletapped=true;
        mypersonalfunc(arg1, arg2, arg3);
    } else {
        tapped = setTimeout(function(){tapclean()}, timer);
        lastclick = elem;
}
}
function tapclean(){
    clearTimeout(tapped);
    tapped="";
    //doubletapper=false;
}

With this you can easily make single tap event, double, triple, fourth,... to infinity and beyond!
Notice: to use element checking, call the function like:
dbtapper(YOURELEMENT), ie: <span onclick="dbtapper(this)">Click Me</span>

It's not forced tho, you can use it without just fine (dbtapper()), but if you need to pass argument into the function make sure you type false for the first argument if you doesn't point it to the element.

Without element pointer: dbtapper(false,yourarguments)
With element pointer: dbtapper(document.getElement*******,yourarguments)
Alternatively, you can use an unique elem arg for each element:
                    dbtapper('omega',yourarg) or dbtapper(69,yourarg)
Though this is not recommended if you have a huge amount of double tapping elements.
1

based on latest jquery docs, double tap implementation

https://gist.github.com/attenzione/7098476

Attenzione
  • 803
  • 9
  • 11
0

technoweenie's jquery.doubletap does and this link: jQuery mobile events finally launched seems to be able to solve the task ...

Luca
  • 7,571
  • 5
  • 41
  • 56
0

You could also use jquery.finger which supports delegated events.

ngryman
  • 5,344
  • 1
  • 24
  • 23
  • 3
    When voting down, please explain why. jquery.finger handles doubletap, which answers the question. – ngryman Jul 18 '14 at 20:33
  • 1
    The downvote reason is clearly explained in the [javascript](http://stackoverflow.com/questions/tagged/javascript) tag: _"Unless another tag for a framework/library is also included, a pure JavaScript answer is expected."_ -- with that in mind, this is not considered a good answer, even though it is most definitely a working solution if someone decides to go with a 60KB library (+plugin) for something that can be done with at most 10 lines of code, including comments. Additionally, link-only answers are discouraged (as the link can go bad), especially when no code is included in the answer. – John Weisz Aug 01 '16 at 09:55
-13

You could use jQuery mobile library to build your application: http://jquerymobile.com/ ; it has plenty of touch events built in. Also you could try jQuery touch library : http://jqtouch.com/

bobek
  • 7,721
  • 8
  • 33
  • 72
  • 1
    Your jQuery Touch solution is good but you should add some code examples to make more clear. – Azeem Hassni Aug 19 '16 at 08:13
  • 1
    Is it just me or are both of those libraries horribly documented with no actual "here's what's available" docs to read? – ChrisH Jun 14 '17 at 16:35