touch events firing twice

12,294

Solution 1

Your issue is that your function is getting triggered twice (once for each event type). See the DEMO here (I used mousedown and click as I am on a desktop right now - but its the same principle).

You need to catch and handle duplicate calls to the event. You could try setting a handled boolean so the click event knows if the touch event handled the event or not (the touch event should be firing first). Something like this...

var handled = false;
$(document).on("touchend click", ".lines-button", function(e){
    e.stopImmediatePropagation();

    if(e.type == "touchend") {
        handled = true;
        handleIt();
    }
    else if(e.type == "click" && !handled) {
        handleIt();
    }
    else {
        handled = false;
    }
});

function handleIt() { 
        if($(this).hasClass("close")){
            $(this).removeClass("close");
            $(".widget1x1Back").next(".actionsHolder3").slideUp("fast", function() {
                $(this).remove();
            });             
        }else{

            var iconsList = $(this).closest(".top1x1").next(".hdnActnLst").find(".iconsHolder3").html();
            $(this).closest(".widget1x1").append(iconsList);
            $(this).closest(".widget1x1").find(".actionsHolder3").hide();
            $(this).closest(".widget1x1").find(".actionsHolder3").slideDown(700,"easeOutBack");
            $(this).addClass("close");  
        } 
}

Solution 2

In most cases the following code will be ok:

$(document).on("touchend click", ".lines-button", function(e){
    if(e.type == 'touchend'){
        $(this).off('click');
    }
});

If touchend works, you may get rid of click.

Share:
12,294
ajmajmajma
Author by

ajmajmajma

Designer and developer in the Boston area. I am passionate about : Web app design and development, Graphic Design, Responsive Web Design, New Media Design, Web Development and Integration, Project Coordination and Print Advertising Collaterals.

 Specialties: Adobe Suite, Responsive Web Development and Design, CMS Installation & Customization, UI/UX design and development, Photo Augmentation & Manipulation, SEO, With development in : Javascript, Angular/Node, React, Ember, D3, web sockets(socket.io), AJAX, jQuery, HTML5, CSS3, SASS/LESS, Yeoman, lodash/underscore, RubyGems, Play, Foundation, Bootstrap, Skeleton, handlebars, PHP, canvas, svg, velocity.js, three.js, MySQUL, MongoDB, Git, SVN. SOreadytohelp

Updated on July 28, 2022

Comments

  • ajmajmajma
    ajmajmajma almost 2 years

    I am having problems on mobile devices/tablets with the events firing twice. When I click the following function, the menu that is supposed to drop down will drop down then immediataly slide back up. It is only an issue with touch devices.

    $(document).on("touchend click", ".lines-button", function(e){
    
        e.stopImmediatePropagation();
        if($(this).hasClass("close")){
            $(this).removeClass("close");
            $(".widget1x1Back").next(".actionsHolder3").slideUp("fast", function() {
                $(this).remove();
            });             
        }else{
    
            var iconsList = $(this).closest(".top1x1").next(".hdnActnLst").find(".iconsHolder3").html();
            $(this).closest(".widget1x1").append(iconsList);
            $(this).closest(".widget1x1").find(".actionsHolder3").hide();
            $(this).closest(".widget1x1").find(".actionsHolder3").slideDown(700,"easeOutBack");
            $(this).addClass("close");
        }
    });
    

    Any input would be great, thanks!!

  • JRulle
    JRulle over 9 years
    Here is an updated codepen.io to show you the event skipping logic
  • griffla
    griffla almost 8 years
    That's a great clean solution.
  • agoldev
    agoldev about 7 years
    That's only a great solution if you are only interested in the first click. Otherwise you have to re-establish the listener again and again.
  • Leo
    Leo over 6 years
    I believe your code needs to be updated? It still uses "e.event" when it should be "e.type", "e.event" returns undefined.
  • Leo
    Leo over 6 years
    True @agoldev, just tested this out and it looses the event.