Javascript 为什么函数不';不要等待另一个函数
我有两个函数,函数A调用函数B对地址进行地理编码并将LatLng对象返回给函数A,但不知何故,函数A并不等待函数B从Google返回结果Javascript 为什么函数不';不要等待另一个函数,javascript,html,function,google-maps,Javascript,Html,Function,Google Maps,我有两个函数,函数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 geo
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,然后继续执行
我原以为它会等待,但根据我对谷歌地图api工作原理的理解,它本身并不“等待”,所以我该怎么办?您的地理编码功能是异步的。它不会等待。它的结果来自一个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
),这在异步函数中更为重要。您的地理代码函数是异步的。它不会等待。它的结果来自一个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
),这在异步函数中更为重要。B
进行异步调用,这意味着无法保证执行顺序。您最好的选择是,在B
中注册的回调函数中初始化此处的之前,A
(长)中的执行将到达测试
基本上,在初始化此处
之后,必须从回调内部调用您想在此处对执行的任何操作。看看jquery,一个跨浏览器的js库,它用延迟的对象形式化了这种延迟执行模式。特别是它附带了一个ajax
方法,在成功和失败时调用用户可定义的回调。B
进行异步调用,这意味着无法保证执行顺序。您最好的选择是,在B
中注册的回调函数中初始化此处的之前,A
(长)中的执行将到达测试
基本上,在初始化此处
之后,必须从回调内部调用您想在此处对执行的任何操作。看看jquery,一个跨浏览器的js库,它用延迟的对象形式化了这种延迟执行模式。特别是它带有一个ajax
方法,在成功和失败时调用用户可定义的回调。对重复的问题表示歉意,但我已经接受了有效的答案,谢谢@jfriend00对重复的问题表示歉意,但我已经接受了有效的答案,谢谢@jfriend00