简体   繁体   English

执行函数完成后调用Javascript函数

[英]Javascript function calling after the executing function finishes

I am working on google Maps API. 我正在使用Google Maps API。 I dont know why the below function is being called after the index++. 我不知道为什么在index ++之后调用下面的函数。 As far as I know ReverseGeocode() should be called first. 据我所知,应该首先调用ReverseGeocode()。 Instead of that it is first incrementing and then calling the function which is creating problems for me. 取而代之的是,它是先递增然后调用该函数,这给我带来了麻烦。 The alert boxes are shown as they are written but the middle function is called after the last line of the function is executed ie (index++). 警报框在编写时显示,但是在执行函数的最后一行(即(index ++))之后调用中间函数。

      function placeMarker(location)
      {
          alert("iiii");
          ReverseGeocode(location.lat(),location.lng());
          alert("jjjk");
          index++;
      }

Here is my ReverseGeoCode 这是我的ReverseGeoCode

  function ReverseGeocode(lat,lng) {     
     var latlng = new google.maps.LatLng(lat, lng);
     geocoder.geocode({'latLng': latlng}, function(results, status)
  {
      if (status == google.maps.GeocoderStatus.OK) 
     {
           if (results[1])
      {

          places[index]=results[0].formatted_address;
          alert(places[index]+"index="+index);
          AddRow('table',results[0].formatted_address);
          document.getElementById("dataa").innerHTML+=results[0].formatted_address+"<br/>";
      }
  }
  else 
  {
    alert("Geocoder failed due to: " + status);
      }
    });
  }

Please Explain. 请解释。 Thanks in advance. 提前致谢。

The alert is inside your callback function, which will execute when geocoder.geocode finishes its calculation. 警报位于您的回调函数内部,该回调函数将在geocoder.geocode完成计算后执行。

geocoder.geocode appears to asynchronous. geocoder.geocode似乎是异步的。 Usually, this means geocoder.geocode will start plodding along with its work somewhere else, while your program continues to its local conclusion. 通常,这意味着geocoder.geocode将开始在其他地方进行工作,而您的程序将继续其本地结论。 When geocoder.geocode later finishes, it will execute your supplied callback function. geocoder.geocode稍后完成时,它将执行您提供的回调函数。

I think that the geocoder.geocode is asynchronous. 我认为geocoder.geocode是异步的。 It is executing your anonymous function somethime later, when the value of index has incremented. index的值增加时,它将稍后执行您的匿名函数。

function placeMarker(location)
 {
 alert("iiii")
 ReverseGeocode(location.lat(),location.lng(),index);
 alert("jjjk");
 index++;

} }

function ReverseGeocode(lat,lng,index) {     
     var latlng = new google.maps.LatLng(lat, lng);
     geocoder.geocode({'latLng': latlng}, function(results, status)
  {
      if (status == google.maps.GeocoderStatus.OK) 
     {
           if (results[1])
      {

          places[index]=results[0].formatted_address;
          alert(places[index]+"index="+index);
          AddRow('table',results[0].formatted_address);
          document.getElementById("dataa").innerHTML+=results[0].formatted_address+"<br/>";
      }
  }
  else 
  {
    alert("Geocoder failed due to: " + status);
      }
    });
  }

In this case, index goes into the local scope of the anonymous function, so it is not overwritten. 在这种情况下, index将进入匿名函数的本地范围,因此不会被覆盖。

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

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