Android/phonegap - 点击响应时间慢
我即将制定出由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我为此使用了
touchend
。即使操作是拖动/滚动
,也会触发touchstart
。I have used
touchend
for this.touchstart
is being triggered even though the action isdrag/scroll
.监听“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/
不要使用按钮。如果将事件添加到 div 中,速度会更快。
Don't use buttons. If you add the event to a div it's faster.
我正在开发计算器,我的第一个想法是从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)
在 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