Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touchend fires on touchstart second time around

Does anyone know why a touchend event would fire during a touchstart event? This only happens the second time around.

A quick code snippet:

function touchstart (event) {
    $(event.target).one('touchend', function () {
        alert('fired');
    }
}

So the first time this is fired it works fine. Second time it fires the alert on touchstart.

http://jsfiddle.net/8SVFR/

Edit:

Looks like this could be an iPhone issue only

like image 570
darylhedley Avatar asked Dec 30 '12 15:12

darylhedley


2 Answers

Turns out...by having an alert fire in a touchend event causes all sorts of problems. When you click 'ok' it fires the touchstart so the touchend gets fired next time you touch the element. Luckily I was using the alert to check my code - so once this was removed my code worked perfectly!

like image 182
darylhedley Avatar answered Nov 12 '22 07:11

darylhedley


Just put code of "touchend" handler in setTimeout with 0ms. Like this:

$(someElement).on("touchend",
function(){
    setTimeout(function(){
    /*Your code*/
    }, 0);
});
like image 44
Serj Avatar answered Nov 12 '22 07:11

Serj