开发者

How to limit Interval time between 2 click in jquery?

开发者 https://www.devze.com 2023-04-09 11:40 出处:网络
How do I limit Interval time between 2 clicks in jQuery? $(\'button\').click(function(){ // do some stuff

How do I limit Interval time between 2 clicks in jQuery?

$('button').click(function(){
  // do some stuff
}

After the first click, how do I wait one second, then the next click will effect. th开发者_运维问答anks.


You could do:

var clicked = false;
$('button').click(function(){
  // do some stuff
  if (!clicked){
      alert('true');
      clicked = true;
      setTimeout(function(){ clicked = false; }, 1000); 
  } else {
      return false;    
  }     
}


Preventing double Ajax requests

$("button").click(function(evt){
    evt.preventDefault();

    // save button context
    var ctx = this;

    if (ctx.preventAjax === false)
    {
        ctx.preventAjax = true;

        // make a request
        $.ajax({
            ...
            complete: function() {
                delete ctx.preventAjax;
            }
        });
    }
});

Not using attributes on elements is faster than using those and checking them on each click.


It seems like you are looking for a way to throttle the clicks. Have a look at the jQuery throttle/debounce plugin by Ben Alman.


After first click you can disable the button untill your ajax finshed.

$('button').click(function(){         
     $('button').attr('disabled',true); //disable the button
     // do some stuff
       $('button').attr('disabled',false);//enable button back .(most probably u need to write it in success event of ajax request)
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消