admin管理员组

文章数量:1401645

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.

/

Edit:

Looks like this could be an iPhone issue only

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/8SVFR/

Edit:

Looks like this could be an iPhone issue only

Share Improve this question edited Dec 30, 2012 at 16:18 darylhedley asked Dec 30, 2012 at 15:35 darylhedleydarylhedley 2581 gold badge8 silver badges23 bronze badges 7
  • Are we talking about mobile phone browser? Works just fine on my Android 4.1 native browser. – try-catch-finally Commented Dec 30, 2012 at 15:55
  • Works fine too in Chrome and Firefox (mobile) on Android 4.1 too. – try-catch-finally Commented Dec 30, 2012 at 15:56
  • Only tried it on iPhone and it fires on touchstart the second time around – darylhedley Commented Dec 30, 2012 at 15:58
  • I've noticed, depending on the Android version and device sometimes a touch does not get exactly recognized, escpeciall on small buttons. Maybe this is the case? Here's an improved fiddle with a bigger button and logging to the documents body using html(): jsfiddle/tuGZx/1 – try-catch-finally Commented Dec 30, 2012 at 15:58
  • Ok what's weird is if you put in an alert into this updated jsfiddle it fires again on the second touchstart. To get this to work you need to hold the button for a bit. Maybe it's just the alert firing it again? – darylhedley Commented Dec 30, 2012 at 16:03
 |  Show 2 more ments

2 Answers 2

Reset to default 5

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!

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

$(someElement).on("touchend",
function(){
    setTimeout(function(){
    /*Your code*/
    }, 0);
});

本文标签: javascriptTouchend fires on touchstart second time aroundStack Overflow