Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在上一次调用成功之前阻止ajax脚本执行_Php_Jquery_Ajax - Fatal编程技术网

Php 在上一次调用成功之前阻止ajax脚本执行

Php 在上一次调用成功之前阻止ajax脚本执行,php,jquery,ajax,Php,Jquery,Ajax,我有一个执行ajax函数的按钮。 有时服务器会延迟,所以用户可能会多次按下它,认为第一次它不工作。。。 ajax的主要功能如下所示: $.ajax({ type: "POST", url: "page.php", dataType: "html", data:"data=data", success: function(){ ajax2(); ajax

我有一个执行ajax函数的按钮。 有时服务器会延迟,所以用户可能会多次按下它,认为第一次它不工作。。。 ajax的主要功能如下所示:

$.ajax({
        type: "POST",
        url: "page.php",
        dataType: "html",
        data:"data=data",
        success:  function(){
                ajax2();
                ajax3();
        }
    });
由于该ajax函数更新db并生成其他2个ajax函数,我需要阻止按钮重新生成主ajax函数。。。 只有当ajax2()和ajax3()完成时,如果按下按钮,必须重新生成ajax函数。
希望能解释好我的问题

第一次激活事件侦听器时,将其解除绑定:

custom_ajax_handler = function(){
    $('#mybutton').unbind('click'); //won't intercept event from now on.

    //here is your ajax call.
    ajax2();
    ajax3();
}

$('#mybutton').click(custom_ajax_handler);
然后在ajax3成功中重新绑定它:

success : function(){
    $('#mybutton').click(custom_ajax_handler);
}
注意,您可能不应该进行那么多ajax调用。
或者至少这样做不会帮助您的服务器解决延迟问题。

禁用该按钮,然后在完成2个ajax后重新启用它

/// the click event
$('yourbutton').prop("disabled",true);
/// show a loading or something....
$.ajax({
    type: "POST",
    url: "page.php",
    dataType: "html",
    data:"data=data",
    success:  function(){
        var ajax1 = ajax2();
        var ajax2 = ajax3();

        $.when(ajax1,ajax2).done(function(risp1,risp2){
            console.log(risp1,risp2);
            $('yourbutton').prop("disabled",false);
            /// hide the loading
        });
    }
});
阅读了解更多信息

试试这个

//before this ajax call disable button
$.ajax({
    type: "POST",
    url: "page.php",
    dataType: "html",
    data: "data=data",
    success: makeAjaxCalls
});

function makeAjaxCalls() {
    var a1 = ajax2();
    var a2 = ajax3();
    $.when(a1, a2).done(function () {
    //enable your button here
    });
}

是的,但是ajax3可以在Ajax2之前完成。您可以在单击时禁用按钮,并在所有ajax调用完成后重新启用。如果ajax调用是同步的,那么理论上,当前面的调用完成时,它们会依次启动。如果我们知道它的用途,那么就更容易为您提供一个示例。是搜索用的吗?然后您可以保存旧的搜索查询并将其与新查询进行比较。如果它们匹配,则不再执行ajax。