Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

cordova - Remove the 300ms delay from phonegap apps

I have been trying to make my app more responsive, but unfortunately nothing seems to help. I am using PhoneGap and hence I am experiencing the famous 300ms delay for touch events.

This makes the app feel very unresponsive and slow which is just not an option.

I saw some libraries like Fastclick.js that solve this issue for jQuery mobile users, but I am not using jQuery mobile. I am making my app just using HTML, CSS, JavaScript and jQuery.

I really need to find a way to get rid of that 300ms delay for my touch clicks. Looking forward to any help I can get.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Christophe Coenraets has addressed this issue in his Top 10 Performance Techniques for PhoneGap Applications. It's the #6 and the video is available on Adobe TV (at the 31st minute).

Basically, the 300ms delay is not a bug or a performance issue, it's a feature necessary to detect the possible double tap.

The solution to get rid of that delay is to use a combination of touch events instead of the click event with something like this :

var trackingClick = false;
var targetElement = null;
var touchStartX = 0;
var touchStartY = 0;
var touchBoundary = 10;

target.addEventListener('touchstart', function(event) {

    trackingClick = true;
    targetElement = event.target;
    touchStartX = event.targetTouches[0].pageX;
    touchStartY = event.targetTouches[0].pageY;

    return true;
});

target.addEventListener('touchend', function(event) {

    trackingClick = false;

    //handle click event
    ...

    return false;
});

target.addEventListener('touchmove', function(event) {
    if (!trackingClick) {
        return true;
    }

    // If the touch has moved, cancel the click tracking
    if (targetElement !== event.target 
        || (Math.abs(event.changedTouches[0].pageX - touchStartX) > touchBoundary 
        || (Math.abs(event.changedTouches[0].pageY - touchStartY) > touchBoundary)) {
        trackingClick = false;
        targetElement = null;
    }

    return true;
});

target.addEventListener('touchcancel', function() {
    trackingClick = false;
    targetElement = null;
});

But that's basically what FastClick is doing (in fact, the above snippet is just a very basic example ripped off of fastclick source code). There are lots of other cases to handle so if you don't feel like implementing your own library, you should really take a closer look at FastClick. It's not only for jQuery mobile users, in fact, it doesn't even need jQuery, it's just a self-contained small footprint library.

Note for jQuery mobile users : you can use FastClick but you should be aware of the built-in similar feature : vclick

tl;dr : use FastClick if you don't have jQuery Mobile


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...