JS 回调函数如何同步执行?

for (var i = 0; i < GetDate.length; i++)
{
var markerPosition = new AMap.LngLat(CurrJD, CurrWD);
var marker = new AMap.Marker({
//map:mapObj,
position: markerPosition, //基点位置
icon: "../images/caryd.gif", //marker图标,直接传递地址url
offset: { x: -8, y: -34} //相对于基点的位置
});

---A程序点 var info = [];
var MGeocoder;
var address;
//加载地理编码插件
mapObj.plugin(["AMap.Geocoder"], function () {
MGeocoder = new AMap.Geocoder({
radius: 1000,
extensions: "all"
});

                 //返回地理编码结果   
                 MGeocoder.getAddress(marker.getPosition());
                 AMap.event.addListener(MGeocoder, "complete",
                     function (data) {

--B程序点 address = data.regeocode.formattedAddress;

                     })
             });

---c程序点 info.push("目前所在位置 :" + address + "");

}

程序一直A_C_B执行,想A_B_C执行,各位大神啊??

不行,地图解码都是异步的,无法控制,只能将B步骤放到回调函数里面,而且无法控制回调返回的循序。。

你一定要按照循序解码,不能用for语句,只能一个个来解码,成功一个后在执行下一个,自己控制解码的循序。

自己控制的话高德地图解码完成必须要能执行回调不管成功失败,要不后续的解码无法完成

你的for循环大概可以改正这样,就可以按照你的ABC来走


    function ExecGetAddr(GetDate, now) {
        //CurrJD, CurrWD这2个参数你应该是从GetDate获取的吧。。没见你用过GetDate参数
        var markerPosition = new AMap.LngLat(CurrJD, CurrWD);
        var marker = new AMap.Marker({
            //map:mapObj,
            position: markerPosition, //基点位置
            icon: "../images/caryd.gif", //marker图标,直接传递地址url
            offset: { x: -8, y: -34} //相对于基点的位置
        });

        //-- -A程序点 
        var info = [];
        var MGeocoder;
        var address;
        //加载地理编码插件
        mapObj.plugin(["AMap.Geocoder"], function () {
            MGeocoder = new AMap.Geocoder({
                radius: 1000,
                extensions: "all"
            });

            //返回地理编码结果   
            MGeocoder.getAddress(marker.getPosition());
            AMap.event.addListener(MGeocoder, "complete",
                     function (data) {

                         //--B程序点 
                         address = data.regeocode.formattedAddress;

                         //---c程序点 ,放到里面来
                         info.push("目前所在位置 :" + address + "");

                         /////////////////////模仿for,没超过继续调用
                         now++;
                         if (now < GetDate.length) ExecGetAddr(GetDate, now);

                         /////////////////////

                     })
        });
    }