Android/phonegap - 点击响应时间慢

发布于 2025-01-06 08:04:02 字数 1392 浏览 1 评论 0原文

我即将制定出由 GhostCoder 提供的解决方案,暗示检测触摸事件而不是单击事件的想法。下面的代码是我目前拥有的,但是有些东西仍然不太正确。它可以在我的主页(非常基本的页面)上运行,但是在实际的游戏页面上它会崩溃:

这是我的代码: 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;
}

主体按钮(首先显示开始按钮,然后单击时显示停止按钮,然后再次开始等)

    <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">

谢谢,

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:

Here is my code:
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.)

    <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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼泪淡了忧伤 2025-01-13 08:04:02

我为此使用了 touchend 。即使操作是拖动/滚动,也会触发touchstart

I have used touchend for this. touchstart is being triggered even though the action is drag/scroll.

月亮邮递员 2025-01-13 08:04:02

监听“touchstart”而不是“click”:)
触摸屏中的点击有点延迟。
http://floatlearning.com/2011/03/developing-better-phonegap-应用程序/

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

孤独陪着我 2025-01-13 08:04:02

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

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

无悔心 2025-01-13 08:04:02

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

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)

琉璃梦幻 2025-01-13 08:04:02

在 Phonegap 应用程序中,单击事件有 300 毫秒的延迟时间。你为什么不使用 Fastclick 库来达到这个目的???我已经尝试过并且效果很好!

https://github.com/ftlabs/fastclick

希望有帮助

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!

https://github.com/ftlabs/fastclick

Hope is helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文