【问题标题】:how come function doesn't wait for another function [duplicate]为什么函数不等待另一个函数[重复]
【发布时间】:2014-03-29 14:06:42
【问题描述】:

我有 2 个函数,函数 A 调用函数 B,它对地址进行地理编码并将 LatLng 对象返回给函数 A,但不知何故,函数 A 不等待函数 B 从 Google 返回结果。

function A() {
    var address = document.getElementById("txtbox").value;
    //geocoding here
    var here = B(address);
    if (here == null) {
        console.log("seems like geocode didn't work, defaulting value");

在函数B中

function B(address) {
    var here;
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
        console.log("geocoding");                                               
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0].geometry.location);
            currentlatlng = results[0].geometry.location;
            lng = currentlatlng.lng();
            lat = currentlatlng.lat();
            here = new google.maps.LatLng(lat, lng);
        } else {
            console.log("Geocode was not successful for the following reason: " + status);
        }
    });

    return here;
}

但似乎控制台的输出是

seems like geocode didn't work, defaulting value
geocoding

因此,似乎函数 A 调用函数 B 然后继续执行它..

我以为它会等待,但根据我对 google maps api 如何工作的理解,它本身并没有“等待”,所以我该怎么办?

【问题讨论】:

  • 为重复道歉,但我接受了有效的答案,谢谢@jfriend00

标签: javascript html function google-maps


【解决方案1】:

您的地理编码功能是异步的。它不等待。它的结果来自 Ajax 调用(“Ajax”中的“A”代表异步)。

您不能以同步方式使用异步函数进行编程。相反,您必须对它们使用异步技术。在这种情况下,您想要在获得地理编码信息之后运行的任何代码都必须在地理编码操作的完成处理程序中执行或调用。在B() 之后就不能执行了。您必须在 B() 内的完成处理程序中执行它。

如果您希望能够将B() 用于多种用途,则可以将回调传递给B(),并在地理编码数据可用时调用该回调。

function A(){
    var address = document.getElementById("txtbox").value;
   //geocoding here
    B(address, function(geocodeData) {
        // use geocode data here
    });
}

function B(address, callback){
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
         console.log("geocoding");                                              
         if (status == google.maps.GeocoderStatus.OK) {
             console.log(results[0].geometry.location);
             var currentlatlng = results[0].geometry.location;
             var lng = currentlatlng.lng();
             var lat = currentlatlng.lat();
             var here = new google.maps.LatLng(lat, lng);
             // call your callback here and pass it the data
             callback(here);
         }else {
             console.log("Geocode was not successful for the following reason: " + status);
             }
        });
}

仅供参考,您的变量可能应该声明为局部变量(在它们前面加上var),这在异步函数中更为重要。

【讨论】:

    【解决方案2】:

    B 进行异步调用,这意味着不保证执行顺序。您最好的选择是,在hereB 中注册的回调函数中初始化之前,执行将在A(长)中到达测试。

    基本上,无论您想对here 做什么,都必须在初始化here 之后从回调内部调用。看看 jquery,一个跨浏览器的 js 库,它用Deferred 对象形式化了这种延迟执行的模式。特别是它带有一个ajax 方法,该方法带有用户可定义的回调,在成功和失败时调用。

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多