How to bind 'touchstart' and 'click' events but not respond to both?

前端 未结 30 2997
抹茶落季
抹茶落季 2020-11-22 14:08

I\'m working on a mobile web site that has to work on a variety of devices. The one\'s giving me a headache at the moment are BlackBerry.

We need to support both key

30条回答
  •  有刺的猬
    2020-11-22 14:28

    If you are using jQuery the following worked pretty well for me:

    var callback; // Initialize this to the function which needs to be called
    
    $(target).on("click touchstart", selector, (function (func){
        var timer = 0;
        return function(e){
            if ($.now() - timer < 500) return false;
            timer = $.now();
            func(e);
        }
    })(callback));
    

    Other solutions are also good but I was binding multiple events in a loop and needed the self calling function to create an appropriate closure. Also, I did not want to disable the binding since I wanted it to be invoke-able on next click/touchstart.

    Might help someone in similar situation!

提交回复
热议问题