简体   繁体   中英

how to listen for two events and only fire one function in jquery mobile?

I have this:

$('li.clickable').live('click tap', function() {
   console.log("hello");
   )};

If I listen like this and click on the list item, two events are firing = I'm getting two "hellos". I need to fire a function with every "click tap", but only once!

Is there any way to do this. I cannot use "one" because then the function will go silent after the first click, isn't it?

Thanks for pointer!

EDIT :
if I omit either click or tap, the function still fires twice. Any way to prevent this?

jQM Docs:

Virtual mouse events
We provide a set of "virtual" mouse events that attempt to abstract away mouse and touch events. This allows the developer to register listeners for the basic mouse events, such as mousedown, mousemove, mouseup, and click, and the plugin will take care of registering the correct listeners behind the scenes to invoke the listener at the fastest possible time for that device. In touch environments, the plugin retains the order of event firing that is seen in traditional mouse environments, so for example, vmouseup is always dispatched before vmousedown, and vmousedown before vclick, etc. The virtual mouse events also normalize how coordinate information is extracted from the event, so in touch based environments, coordinates are available from the pageX, pageY, screenX, screenY, clientX, and clientY properties, directly on the event object.

vclick

Normalized event for handling touchend or mouse click events. On touch devices, this event is dispatched AFTER vmouseup.

Try using:

$('li.clickable').live('vclick', function() {
   console.log("hello");
)};

Also you can test which event is firing

$('li.clickable').live('click tap', function(e) {
    console.log("Which Event: "+e.which + " Event Type: "+e.type);
)};

Since your action also fires twice if only listening to click you might be running into some user or system weirdness.

We had all sorts of problems when capacitive touchscreen started to become widespread and people (eg in gloves) would start double-triggering. What helps against a user-doubletap might help against a system-doubletap:

//global scope:
var triggertimeouts={}

and

$('li.clickable').live('click tap', function() {
   if (!triggertimeouts.myclicktap) {
      triggertimeouts.myclicktap=window.setTimeout('triggertimeouts.myclicktap=false;',250);
      console.log("hello");
   }
   )};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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