简体   繁体   English

FastClick在Phonegap项目中抛出.on('click')循环计数

[英]FastClick throws off .on('click') loop count in Phonegap project

I have the following attached to an href tag: 我在href标签上附加了以下内容:

$('body').on('click', '.classidentifier', function(e) 
    {
     DoSomething();
    }); 

The DoSomething() function includes an incrementing loop that's supposed to fire six times: DoSomething()函数包含一个增量循环,该循环应该触发六次:

var DoSomething = function() {
    if (ClickCounter < 6) {
    ClickCounter++  
    DoSomethingElse();
    };  
    if (ClickCounter == 6 ){
    BailYouAreDone();
    ClickCounter = 0; }
    }

All worked fine in a browser environment with actual mouse clicks. 在实际的鼠标单击中,所有这些在浏览器环境中都可以正常工作。 It also worked fine in a compiled PhoneGap app. 它在已编译的PhoneGap应用程序中也可以正常工作。

But I was sick of the 300ms lag that .on('click') events incur in a touchscreen environment, so I installed the FastClick.js library, which monitors for the touchend event and sends a synthetic click to .on('click') events. 但是我厌倦了.on('click')事件在触摸屏环境中产生的300ms延迟,因此我安装了FastClick.js库,该库监视touchend事件并将合成点击发送到.on('click' )事件。

But now the DoSomethingElse() function fires > 6 times: sometimes 7, sometimes 8, sometimes more, seemingly dependent on how fast I press the href link. 但是现在DoSomethingElse()函数触发> 6次:有时是7次,有时是8次,有时还会更多,这似乎取决于我按href链接的速度。 I've tried trapping for ClickCounter > 6 in the DoSomethingElse() routine, but no joy. 我已经尝试在DoSomethingElse()例程中捕获ClickCounter> 6,但没有任何乐趣。 What am I doing wrong? 我究竟做错了什么?

Elsewhere here there is a tip on ensuring that .on('click') events only fire once: 在其他地方,这里有一个技巧可确保.on('click')事件仅触发一次:

$(element).off().on('click', function() {
// function body
});

Try that. 试试看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM