如何在javascript中声明变量
可能是因为另一个在回调中?这取决于.geocode如何处理它,但很可能是ajax回调。可能是因为另一个在回调中?这取决于.geocode使用它做什么,但很可能是ajax回调。如果geo.geocode是ajax请求,那么它后面的代码在执行之前不会等待响应返回 因此,代码运行不正常。第二个警报开火,因为没有任何东西可以阻止它开火。第一个在接收到AJAX响应时触发 如果有其他依赖于响应的代码,则将该代码放入函数中,并从geo.geocode的回调中调用它 如果geo.geocode是一个AJAX请求,那么它后面的代码在执行之前不会等待响应返回 因此,代码运行不正常。第二个警报开火,因为没有任何东西可以阻止它开火。第一个在接收到AJAX响应时触发 如果有其他依赖于响应的代码,则将该代码放入函数中,并从geo.geocode的回调中调用它 地理编码功能是异步的 您传递给它的回调在调用geocode后运行一段时间;地理编码功能本身不能等待响应。地理编码功能是异步的 您传递给它的回调在调用geocode后运行一段时间;geocode函数本身不会也不能等待响应如何在javascript中声明变量,javascript,jquery,Javascript,Jquery,可能是因为另一个在回调中?这取决于.geocode如何处理它,但很可能是ajax回调。可能是因为另一个在回调中?这取决于.geocode使用它做什么,但很可能是ajax回调。如果geo.geocode是ajax请求,那么它后面的代码在执行之前不会等待响应返回 因此,代码运行不正常。第二个警报开火,因为没有任何东西可以阻止它开火。第一个在接收到AJAX响应时触发 如果有其他依赖于响应的代码,则将该代码放入函数中,并从geo.geocode的回调中调用它 如果geo.geocode是一个AJAX请求
$("#btnsub").click(function () {
var add = $("#txtname").val();
var obj;
var geo = new google.maps.Geocoder;
geo.geocode({ 'address': add }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
obj = results[0].geometry.location;
obj = convert(obj);
alert(obj);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
alert(obj);
// why this alert run first instead of the upper alert()
});
$("#btnsub").click(function () {
var add = $("#txtname").val();
var obj;
var geo = new google.maps.Geocoder;
geo.geocode({ 'address': add }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
obj = results[0].geometry.location;
obj = convert(obj);
alert(obj);
// Call a function that relies on the response
// and pass the obj to it.
someOtherFunction( obj );
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});
function someOtherFunction( data ) {
alert( data );
// do something with the obj (referenced as data)
}