简体   繁体   English

jQuery 点击进行多次 AJAX 调用

[英]jQuery click makes multiple AJAX calls

I've got multiple jQuery scripts working together.我有多个 jQuery 脚本一起工作。 One binds the click actions to the elements, another handles the specific functions.一个将点击动作绑定到元素,另一个处理特定的功能。

Although trying to stop multiple calls in different ways i can still fire off two calls by clicking really fast.尽管尝试以不同的方式停止多个呼叫,但我仍然可以通过快速单击来关闭两个呼叫。

After the success call rebinds the action its also possible to double the number of calls each time it comes back, but this seems to be the consequence of being able to call it twice in the first place.在成功调用重新绑定操作后,每次返回时调用次数也可能加倍,但这似乎是首先能够调用它两次的结果。

Any suggestions to make sure takeAction can only be called upon once?有什么建议可以确保只能调用一次takeAction吗?

Here's a snippet of my code:这是我的代码片段:

Action-click binder:动作点击活页夹:

var actions = 
{
    pressed: false,
    bind: function()
    {
        $(".battleAction").off('click').one('click', function(event) {
            if (actions.pressed) return;
            actions.pressed = true;
            $(".battleAction").off('click');
            var idoption = $(this).attr('data-battle-option');
            battle.takeAction({    idoption: idoption   });
        });
    }
}

Battle functions:战斗功能:

var battle =
{
    takeAction: function( action )
    {
        battle.a = {
            idoption: null
        };

        $.extend( battle.a, action );

        $.ajax({
            url: "url/to/post",
            type: "post",
            data: { option: battle.a.idoption },
            dataType: "json",
            success: function(data) {
                actions.bind();
            }
        });
    }
}

And the HTML buttons that get the click event:以及获取点击事件的HTML按钮:

<button type="button" class="btn btn-default battleAction" data-battle-option="1">
<span class="glyphicon glyphicon-star" aria-hidden="true"></span>action 1</button>

<button type="button" class="btn btn-default battleAction" data-battle-option="2">
<span class="glyphicon glyphicon-star" aria-hidden="true"></span>action 2</button>

<button type="button" class="btn btn-default battleAction" data-battle-option="3">
<span class="glyphicon glyphicon-star" aria-hidden="true"></span>action 3</button>

The problem seems to be with re-assigning the click after the AJAX call.问题似乎在于在 AJAX 调用后重新分配点击。 In that small timeframe I am able to launch two calls.在这么短的时间内,我可以发起两次通话。

You can disable the button after clicking and re-enable it after AJAX call:您可以在单击后禁用该按钮,并在 AJAX 调用后重新启用它:

Action-click binder:动作点击活页夹:

$(".battleAction").on('click', function(event) {
  $(this).prop("disabled", true);
  var idoption = $(this).attr('data-battle-option');
  battle.takeAction({    idoption: idoption   });
});

Battle functions:战斗功能:

var battle =
{
    takeAction: function( action )
    {
        battle.a = {
            idoption: null
        };

        $.extend( battle.a, action );

        $.ajax({
            url: "url/to/post",
            type: "post",
            data: { option: battle.a.idoption },
            dataType: "json",
            success: function(data) {
                $(".battleAction").prop("disabled", false);
            }
        });
    }
}

It seems you might want to move the .off to the first thing in the handler so that is always gets "off" even if you return true:似乎您可能希望将.off移动到处理程序中的第一件事,这样即使您返回 true,它也总是“关闭”:

$(".battleAction").off('click').one('click', function(event) {
   $(".battleAction").off('click');
   if (actions.pressed) return;
   actions.pressed = true;
   var idoption = $(this).attr('data-battle-option');
   battle.takeAction({
     idoption: idoption
   });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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