Detect double tap on ipad or iphone screen using javascript
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();
}
One basic idea is to do it like this:
In order to create a double-tap (or double click) event you need to create code on the
onClick
event.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.This means that your
onClick
event should only launch theonClick
event after asetTimeout()
is acknowledged.So the basic code would launch the function attached to the
onClick
event using asetTimeout()
command. The first click says "Start timer + run function usingsetTimeout()
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 thesetTimeout()
usingclearTimeout()
and then launch a completely different function (the function you wanted to launch for double tab/click)Stopping default action? - Probably somthing like
stopPropagation()
orcancelBubble()
would do the trick. Honestly, I don't know but that's where I'd start researching.