繁体   English   中英

等待JS函数中的异步调用结束。 (jQuery.Deferred吗?)

[英]Wait for the end of an asynchrounous call in a JS function. (jQuery.Deferred ?)

这是我的(简单)问题:

我有一个Javascript函数,该函数调用外部API以异步获取一些结果。 我需要等待这些结果,因为我想对它们进行一些测试以确定它们是否有效,但是Deferred对我来说非常复杂,我无法成功。

这是我所做的:

$("#step-content").steps({
    //some parameters
    onStepChanging: function(event, currentIndex, newIndex) {
        verifyAddress().done(function(test) {
            if($("#hdnLatitude").val() == "" || $("#hdnLongitude").val() == "")
                test = false;
            else
                test = true;

            console.log(test); // test is true or false, that's good
            return test;
        });

        console.log(test); // test is always empty here

        //Here, I just need to do return true or return false to block step-changing if there is an error.
        return test;
    }
});

基本上,这是我的verifyAddress函数:

function verifyAddress() {
    var r = $.Deferred();

    var geocoder = new google.maps.Geocoder();
        if (geocoder) {
            var adressToGeocode = /* Get the address to geocode */

            geocoder.geocode({ 'address': adressToGeocode }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) 
                {
                    //Save the lat/lng returned in #hdnLatitude and #hdnLongitude
                }

                r.resolve();
            });
        }
    //Not sure where to place the return r ; try elsewhere but no success
    return r;
}

所以我需要的是等待verifyAdress()的结束并获取#hdnLatitude#hdnLongitude ,并在onStepChanging事件中返回false的true,以确定是否可以进行下一步(地址确定)而不是(地址为错误)

我正在使用这个SO问题来更好地理解Deferred ,但我无法成功。
有人可以帮助我吗?

非常感谢

您的功能应如下所示:

function verifyAddress() {
    var r = $.Deferred();

    //call Google server to geocode my address. The call is asynchrounous.
    //The results are put in #hdnLatitude and #hdnLongitude fields.
    //The fields are correctly filled.
    googleFunc(someParam, function done(){
      r.resolve();
    });

    return r;
}

您必须解决Google伺服器函式会呼叫某些已done回叫中的延迟,必须有一些回叫功能来处理结果。

var test = false; //Can't change step until allowed in async function   
$("#step-content").steps({
    //some parameters
    onStepChanging: function(event, currentIndex, newIndex) {
    if (!test)
      verifyAddress().done(function() {
            if($("#hdnLatitude").val() == "" || $("#hdnLongitude").val() == "")
                test = false;
            else
                test = true;

            console.log(test); // test is true or false, that's good

            //Auto move to next step if test is true
        });


        //Here, I just need to do return true or return false to block step-changing if there is an error.
        return test;
    }
});

好的,正如@Bergi所说,这个插件似乎不可能。

因此,我绕过了这个问题,用一个伪造的“ Next按钮代替了这个按钮,它调用了我的异步事件。 在此事件结束时,如果结果确定,我将我克隆的原始按钮替换为“ Next按钮,并在其上触发一个click事件,以传递到下一步,而无需用户单击两次。

不知道它是否真的很干净,但是可以满足我的需求

暂无
暂无

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

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