Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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
Javascript 等待异步调用不使用ajax_Javascript_Jquery - Fatal编程技术网

Javascript 等待异步调用不使用ajax

Javascript 等待异步调用不使用ajax,javascript,jquery,Javascript,Jquery,在输入模板中的select present之前,我使用deferred等待大约两个函数完成(它们输入模板并显示结果) 当显示选项卡时,我会这样做 var notAssociateContact = getNotAssociateContact(); var associateContact = getAssociateContact(); $.when(notAssociateContact, associateContact).then(assignLocationToSelector($(

在输入模板中的select present之前,我使用deferred等待大约两个函数完成(它们输入模板并显示结果)

当显示选项卡时,我会这样做

var notAssociateContact = getNotAssociateContact();
var associateContact = getAssociateContact();

$.when(notAssociateContact, associateContact).then(assignLocationToSelector($("select[name=locationIds\\[\\]]")));

function getNotAssociateContact() {

    var deferred = $.ajax({
        type: "GET",
        url: "http://localhost:8080/rest/contacts/notassociatedto/" + $("#lodgerId").val(),
        success: function(data, status, jqXHR) {

            $("#lodgerContactAvailableDivTemplate").empty();
            if (data.length != 0) {
               $("#lodgerContactAvailableDivTemplate").append(templateLodgerContactAvailable(data));
                $('#lodgerContactAvailableTableResult').bootstrapTable();
            }
        },
        error: function(jqXHR, status) {
            // error handler
            alert("error " + jqXHR + " -  " + status);
        }
    }).then(function(response) {
        // optional callback to handle this response 
    });
    return deferred;

}


function getAssociateContact() {
    var deferred = $.ajax({
        type: "GET",
        url: "http://localhost:8080/rest/lodgers/" + $("#lodgerId").val() + "/contacts",
        success: function(data, status, jqXHR) {
            $("#lodgerContactDivTemplate").empty();
            if (data.length != 0) {
                $("#lodgerContactDivTemplate").append(templateLodgerContact(data));
                $('#lodgerContactTableResult').bootstrapTable();
                //  $('#lodgerContactTableResult tr').bind("dblclick", null, contactReferenceSelectedRow);
            }
        },
        error: function(jqXHR, status) {
            // error handler
            alert("error " + jqXHR + " -  " + status);
        }
    }).then(function(response) {
        // optional callback to handle this response 
    });
    return deferred;
}
我没有得到任何错误,但是assignLocationToSelector与runned不一样。 Select为空

在控制台中,如果我运行

assignLocationToSelector($("select[name=locationIds\\[\\]]"));
选择是否正确进给

当我调试并到达$时,我看到两个ajax调用挂起

因此,似乎延迟函数存在问题。

您的
$。when()。then()
需要传递一个函数引用。您正在立即调用函数,然后传递返回结果。它立即执行函数,而不允许
$.when()
稍后调用它

更改您的
$.when()
语句:

$.when(notAssociateContact, associateContact).then(assignLocationToSelector($("select[name=locationIds\\[\\]]")));
为此:

$.when(notAssociateContact, associateContact).then(function() {   
   assignLocationToSelector($("select[name=locationIds\\[\\]]"));
});