1

您好,我对这个论点很困惑:我知道在 javascript 函数中有时会以异步方式执行,这是我的问题。我有一个名为

function createPopupHour() 

此函数创建一个 html 选择元素,它不返回任何内容。我在请求成功部分的 $.AJAX 请求中调用此函数。

$.ajax({
            url:"responseregistrodocente.php",
            data:{
                operazione:'caricaAssenza',
                idAssenza:id_array[3],
                codiceFiscale: id_array[0],
                data:id_array[1],
                tipo:id_array[2]
                },
            type:"POST",
            dataType:"json",
            success: function (jsonObject) {
            createPopupHourSelect()
            //other code
            }); 

            },
            error: function(error){
                //XMLREQQUESTOBJECT
                alert(error.responseText);
                location.reload();
            },
            cache:false,
            ifModified:false
        });

问题是当我调用该函数时,其他代码不会注意到我的函数结束。我知道在 jquery 中有“延迟对象”,也许我需要我的函数创建一个延迟对象,并将其返回给代码。但是sintax怎么样:?还是有另一种更简单、更干燥的解决方案???

这样的事情是正确的吗?

function createPopupHour select(){ //staff to do
return $.deferred();//it's in pending state
}

和 $.ajax

$.ajax({
                url:"responseregistrodocente.php",
                data:{
                    operazione:'caricaAssenza',
                    idAssenza:id_array[3],
                    codiceFiscale: id_array[0],
                    data:id_array[1],
                    tipo:id_array[2]
                    },
                type:"POST",
                dataType:"json",
                success: function (jsonObject) {
                var defered=createPopupHourSelect()
                defered.then(function{//other code])
                        defered.resolve();
                }); 

                },
                error: function(error){
                    //XMLREQQUESTOBJECT
                    alert(error.responseText);
                    location.reload();
                },
                cache:false,
                ifModified:false
            });
4

1 回答 1

1

是的,other code需要驻留在一个回调函数中,该回调函数将在弹出事件完成时执行,就像在 ajax 完成时执行弹出启动代码一样。您可以使用原始回调,也可以使用更强大的Promise模式。

jQueryDeferred对象的语法是

function …() {
    var def = $.Deferred();
    // start asynchronous task
         // when the task is done (in the future), call
         def.resolve(…); // optionally with results
    // and right now do
    return def.promise();
}

由于$.ajax确实也返回了一个承诺,因此您可以使用链接 via .then(假设createPopUpHourSelect在上述模式中):

$.ajax({
    url:"responseregistrodocente.php",
    data:{…},
    type:"POST",
    dataType:"json",
    cache:false,
    ifModified:false
})
.fail(function(error){
    alert(error.responseText);
    location.reload();
})
.then(createPopupHourSelect) // gets passed the parsed JSON
.then(function(result) { // gets passed the resolve arguments from the popup
    // other code
});

如果您还需要其他代码中的 ajax 响应并且不想通过弹出函数传递它,请使用

.then(function(json) {
    return createPopupHourSelect(…)
    .then(function(popupResults) {
         // other code
    });
}) /* returns a promise that resolves with result of other code
.then(…) */
于 2013-10-14T14:01:20.867 回答