简体   繁体   中英

How to stop function propagation after first click with jQuery

I have some click function... whole action is fine but if i fastly doubleclick at call button some elements generates two times. Can you tell me how to stop function propagation after first click? Much thx for help, and this is my code:

$('#start_panel ul li').click(function(event){
    if($(this).hasClass('eng')){
       ...
    }
    else if($(this).hasClass('ger')){
       ...
    }
    else if($(this).hasClass('pln')){
       ...
    }
    event.stopPropagation();
});
var clicked = false;
$('#start_panel ul li').click(function(event){
   if (clicked) return;
   clicked = true;

Or use the one function (slightly different : now only one click per li element will be handled) :

 $('#start_panel ul li').one('click', function(event){

Try this instead:

var myClicker = function(event){
    var self = event.currentTarget;
    if($(self).hasClass('eng')){
       ...
    }
    else if($(self).hasClass('ger')){
       ...
    }
    else if($(self).hasClass('pln')){
       ...
    }
    $(self).one('click', myClicker);
});
$('#start_panel ul li').one('click', myClicker);
 var i=0;
$('#start_panel ul li').click(function(event){
 if(i==1){
  i=0;
 return false;
  }
if($(this).hasClass('eng')){
   ...
}
else if($(this).hasClass('ger')){
   ...
}
else if($(this).hasClass('pln')){
   ...
}
 i++;


 });

To clear previous request use Stop(true,true)

$('#start_panel ul li').stop(true,true).click(function(event){
    if($(this).hasClass('eng')){
        ...
    }
    else if($(this).hasClass('ger')){
       ...
    }
    else if($(this).hasClass('pln')){
       ...
    }
    event.stopPropagation();
});

You can unbind the click event after the first click:

$('#start_panel ul li').click(function(event){
    $(this).unbind('click'); 
    if($(this).hasClass('eng')){
       ...
    }
    else if($(this).hasClass('ger')){
       ...
    }
    else if($(this).hasClass('pln')){
       ...
    }
    event.stopPropagation();
});

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