javascript Android/phonegap - 点击响应时间慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9309433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:14:59  来源:igfitidea点击:

Android/ phonegap - Click response time slow

javascriptandroidcordovatimer

提问by Raj Labana

I am close to working out a solution courtesy of ghostCoder hinting at the idea of detecting the touch event rather than the click event. This below code is what I currently have, however something is still not quite right. It works on my homepage (very basic page), however with the actual game page it breaks:

我即将制定出由 ghostCoder 提供的解决方案,暗示检测触摸事件而不是点击事件的想法。下面的代码是我目前所拥有的,但是有些地方仍然不太正确。它适用于我的主页(非常基本的页面),但是在实际游戏页面上它会中断:

Here is my code: JAVASCRIPT:

这是我的代码:JAVASCRIPT:

var b=document.getElementById('STOP'),start=0;

//Check for touchstart
if('ontouchstart' in document.documentElement) 
{
    document.getElementById("notouchstart").style.display = "none";
}

//Add a listener that fires at the beginning of each interaction
[b].forEach(function(el){el.addEventListener('touchstart',interact);});

//Add the event handlers for each button
b.addEventListener('touchstart',highlight);

//Functions Store the time when the user initiated an action
function interact(e) 
{
    start = new Date();
}

//Highlight what the user selected and calculate how long it took the action to occur
function highlight(e) 
{
    e.preventDefault();
    e.currentTarget.className="active";
    if(start)
    {
        alert("test")
    }
    start = null;
}

BODY BUTTONS (firstly displays start button then when clicked displays stop button, then start again etc.)

BODY BUTTONS(首先显示开始按钮,然后单击时显示停止按钮,然后重新开始等)

    <INPUT TYPE="button" style="background:url(images/Start_Btn.png); background-color:transparent; width:150px; height:186px; border:none; cursor:pointer;" id="START" onClick="startBTN();">
    <INPUT TYPE="button" style="background:url(images/Stop_Btn.png); background-color:transparent; width:150px; height:186px; border:none; cursor:pointer;" id="STOP">

Thanks,

谢谢,

采纳答案by ghostCoder

Listen for ‘touchstart' instead of ‘click' :)
click is a bit delayed in touchscreens. http://floatlearning.com/2011/03/developing-better-phonegap-apps/

聆听 'touchstart' 而不是 'click' :)
在触摸屏中点击有点延迟。 http://floatlearning.com/2011/03/developing-better-phonegap-apps/

回答by alexishacks

I have used touchendfor this. touchstartis being triggered even though the action is drag/scroll.

我已经用于touchend此。touchstart正在被触发,即使操作是drag/scroll

回答by bernard

Don't use buttons. If you add the event to a div it's faster.

不要使用按钮。如果您将事件添加到 div,它会更快。

回答by Grit

I am working on a calculator and my first idea was to start with phonegap. Now I strongly recommend not to do that when button presses are time critical. Even with disabling all extra touch handlers and setting touchstart directly to the div: it's just too slow. (touchend won't do by the way, it's called when you release the finger from the button)

我正在研究一个计算器,我的第一个想法是从 phonegap 开始。现在我强烈建议不要在按钮按下时间紧迫的情况下这样做。即使禁用所有额外的触摸处理程序并将 touchstart 直接设置为 div:它也太慢了。(顺便说一下,touchend 不会做,当您从按钮上松开手指时会调用它)

回答by ira

In a Phonegap application the click event have a 300ms of delay time. Why don't you use Fastclick library for this pourpose??? I have tried it and works great!

在 Phonegap 应用程序中,单击事件有 300 毫秒的延迟时间。你为什么不使用 Fastclick 库来做这个倾诉???我试过了,效果很好!

https://github.com/ftlabs/fastclick

https://github.com/ftlabs/fastclick

Hope is helpful

希望有帮助