请看看这段代码。我期待它:jquery以奇怪的顺序执行
- 执行,节省了两个值(
latitude
和longitude
) - 使用这些值在这里= ajax的
var center = new google.maps.LatLng(resplat, resplon);
的问题是,代码不按此顺序执行。它“跳过”最初的Ajax,并在其他代码运行时首先回到它。这使得我的两个值无用。有人可以解释代码执行顺序背后的逻辑吗?
function Initialize() {
$.ajax({
type: "GET",
url: 'http://ip-api.com/json',
dataType: "json",
success: function (resp) {
resplat = resp.lat;
resplon = resp.lon;
}
});
// Google has tweaked their interface somewhat - this tells the api to use that new UI
google.maps.visualRefresh = true;
var center = new google.maps.LatLng(resplat, resplon);
// These are options that set initial zoom level, where the map is centered globally to start, and the type of map to show
var mapOptions = {
zoom: 15,
center: center,
mapTypeId: google.maps.MapTypeId.G_NORMAL_MAP
};
尝试添加'异步:FALSE'使AJAX保持脚本,直到收到响应。 – user3154108
@ user3154108 [该选项已弃用。](http://stackoverflow.com/q/11448011/1420197) –
这很好理解! (我个人被迫与1.10仍然工作>>) – user3154108