在执行函数完成后调用 Javascript 函数
Javascript function calling after the executing function finishes
我正在研究谷歌地图API。 我不知道为什么在索引++之后调用下面的函数。据我所知,应该首先调用ReverseGeocode()。 取而代之的是,它首先递增然后调用给我带来问题的函数。 警报框在编写时显示,但在执行函数的最后一行(即 (index++))后调用中间函数。
function placeMarker(location)
{
alert("iiii");
ReverseGeocode(location.lat(),location.lng());
alert("jjjk");
index++;
}
这是我的反向地理代码
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);
}
});
}
请解释一下。提前谢谢。
警报位于回调函数中,该函数将在geocoder.geocode
完成计算时执行。
geocoder.geocode
似乎是异步的。 通常,这意味着geocoder.geocode
将开始在其他地方进行工作,而您的程序将继续其本地结论。 稍后geocoder.geocode
完成后,它将执行您提供的回调函数。
我认为geocoder.geocode
是异步的。它会在稍后执行您的匿名函数,当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);
}
});
}
在这种情况下,index
进入匿名函数的本地范围,因此不会被覆盖。
相关文章:
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 如何在Javascript函数调用中循环变量
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- 如何远程检查JavaScript应用程序的函数调用堆栈
- javascript函数调用不起作用
- 为什么这个函数调用会破坏程序并导致未定义的变量
- 如何通过函数调用设置图像的src
- 从全局函数调用Ember控制器上的方法
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- JavaScript函数调用(arg1)(arg2)
- 打印链接时,将javascript函数调用到链接中
- 在函数调用中封装数据除了隐藏数据之外还有什么优点
- 无法从JavaScript中的函数调用对象属性
- 对中的函数调用进行排序是回调的唯一方法
- 函数调用方法有什么用
- Javascript:JSHint:应为赋值或函数调用,但实际看到的却是表达式
- HTML5(Bootstrap)通过函数调用运行动画
- 函数调用不起作用
- 函数中的Javascript函数调用