在javascript中更新全局变量
Update global variable in javascript
我在javascript中有一个全局变量,比如
var EventLocation = {
'center' : '35.59214,-121.046048',
'zoom' : 10
};
现在,在一个函数中,我们将这个变量更新为
var geocoder = new google.maps.Geocoder();
var address = $j('#EventLocation').text(); //record.Location;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
EventLocation.center = new google.maps.LatLng(latitude, longitude);
//onSuccessMaps(latitude,longitude);
} else {
alert('Fail to find location');
}
});
但在另一个函数EventLocation.center中,它不更新,它将以前的值作为('35.59214,-121.046048')
。如何解决此问题?
地理代码调用是异步的,因此代码将在等待响应时继续运行。实际上,在处理响应之前,您必须将控件返回到浏览器。
这意味着任何需要坐标的方法都必须从成功回调方法中调用:
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
EventLocation.center = new google.maps.LatLng(latitude, longitude);
// here the coordinates _are_ set
// this is where you put the code that needs to use the coordinates
} else {
alert('Fail to find location');
}
});
// here the coordinates are _not_ set yet
相关文章:
- 更新后,父视图未在 Electron 中的
上设置全局变量 - 用javascript更新事件函数中的全局变量
- 从函数更新全局变量
- 全局变量在 javascript 中未更新
- 与javascript全局变量范围和更新混淆
- 无法多次更新 jQuery 中 Ajax 调用的全局变量
- 在 NodeJS 中使用和更新全局变量是否安全?
- Javascript 中的全局变量未更新
- 更新 jQuery 中的全局变量
- 在 Javascript 中本地更新全局变量
- AJAX 请求无法读取更新的会话/全局变量
- 在 JavaScript 中回溯,无法更新全局变量
- 节点.JS全局变量未保持更新
- 更新全局变量javascript/html
- JavaScript更新全局变量
- 通过回调更新全局变量
- 为什么我的Javascript全局变量只更新一次?jQuery最后一个子选择器错误
- 在javascript中更新全局变量
- JavaScript全局变量未更新
- 如何在WSO2ESB中创建全局变量-必须是可更新的