Daniel Benedykt
Daniel Benedykt

Reputation: 6553

Event firing multiple times (or even looping) on PhoneGap - Jquerymobile app

I have an app that was developed using Phonegap and JqueryMobile.

This app has been downloaded around 15.000 times total in iOS, Android and iPhone. The code is exactly the same on all platforms.

Once in a while the backend server overloads and when I look at the logs I see that one user is sending hundreds of times the same call. (the users are real persons and I have talk to them about the issue, so its not bot or anything like that)

For me it seems that the either the click event is looping or the server call is looping but could not detect the reason why.

This has happen to 3 users out of 15.000 (as far as I know), and the users have used the app many times before the issue happened. The issue happened on Android and iOS so it seems to me that there is an issue on the jquerymobile/javascript side.

Any idea what could have cause this issue?

Upvotes: 2

Views: 1731

Answers (3)

Daniel Benedykt
Daniel Benedykt

Reputation: 6553

I ended up disabling the button after the first touch and that fixed the issue.

It seems that the main problem was tapping the button twice, but for some reason I could not detect after that, it entered an infinite loop.

Upvotes: 2

Pablo
Pablo

Reputation: 1682

I'd say first watch out for design issues in your js/DOM generation.

When you bind an event that has already been bound, jquery will bind it again without checking if that event has already been bound. That is fine if you want to attach multiple event functions to the same event.

Any way, there are several ways to solve this. One is to unbind the event before binding it, with $.off(), eg.

$("#myDiv").off("click").on("click", function(e) {
    // ...
})

another is to check inside the event function if the event has already been fired, eg:

$("#myDiv").on("click",function(e) {
  if(e.handled !== true) {
      alert('Clicked');
      e.handled = true;
  }
})

You can find more solutions with their pros and cons here

Upvotes: 7

QuickFix
QuickFix

Reputation: 11721

do you do event.preventDefault() and event.stopPropagation() in the onclick function? (without that, browsers behaviour may vary a lot)

Other thought, it may be usefull to hide or disable the buttons at the start of the onclick functions to avoid users from doing multiple clicks.

I'm sure you're already doing all this, but just in case...

Upvotes: 0

Related Questions