jQuery animation: Ignore double click

浪尽此生 提交于 2019-11-30 20:16:42

Just check if element is already animating:

$('a#right').click( function () {
    if ($(this).is(':visible') && !$('#slide').is(':animated')) {
        $('#slide').animate({right: '+=257'}, 400, function () {
            slide_button();
        });
    }
});    

You can use one() in jquery:

$('a#right').one("click", function () {
    slide($(this));
});

function slide(obj) {
    if (obj.is(':visible')) {
        $('#slide').animate({right: '+=257'}, 400, function () {
            slide_button();
            $('a#right').one("click", function () {
              slide($(this));
            }
        });
}

After the animation is completed, the click event is bound to the link again and it can be executed at most once.

Stuart Burrows

I normally get around this by setting a global variable and run an if like

 clicked = true;
 $(div).click(function(){
   if (!clicked){
       clicked = true;
       Your function with a callback setting clicked to false
   }
   ....
 })

You could .unbind() the click event once clicked which will prevent it from executing multiple times:

$('a#right').click(function () {
    $(this).unbind('click');
    ...
});

Once the animation completes you could rebind it if you need to be able to click again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!