JS变量“;消失”;没有理由,变得空虚
JS variable "disappear" with no reason and gets empty
我正试图从Google Geocoder中获取lat和long信息,但尽管在正在进行的调用中正确提取了信息,但由于某种原因,js变量在收到关于这段代码的第二个警报("if"条件之外的警报)后立即变空:
var map, marker, latLong;
var geocoderlat = new google.maps.Geocoder();
var addresslat = '<?php echo str_replace(" ","+",$address);?>';
function initMap() {
geocoderlat.geocode( { 'address': addresslat}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latLong = results[0].geometry.location;
alert("Inside IF: "+latLong);
} else {
latLong = '<?php echo $latitude.",".$longitude;?>';
}
})
alert("Outside: "+latLong);
var isDraggable = !('ontouchstart' in document.documentElement);
var mapOptions = {
center: new google.maps.LatLng(latLong),
zoom: 12,
(...)
显然,除了这一部分之外还有很多代码,但在这种情况下,代码的其余部分并不重要,因为这个变量在我的代码中再也没有使用过(我甚至用代理Ransack搜索了所有文件,所以请确保这个变量没有在其他地方使用)。
编辑:
好吧,这真的,真的很奇怪。。。
function getGeoLatLong(addresslat) {
geocoderlat.geocode( { 'address': addresslat}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
} else {
return '<?php echo $latitude.",".$longitude;?>';
}
})
}
alert("Outside function: "+getGeoLatLong(addresslat));
这仍然会在警报中返回"未定义"。
geocoderlat.geocode
需要更多的时间执行,而代码执行已经达到第二个警报。您需要使用setTimeout
来检查结果或尝试此操作。
function initMap() {
geocoderlat.geocode( { 'address': addresslat}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latLong = results[0].geometry.location;
alert("Inside IF: "+latLong);
alert("Outside: "+latLong);
var isDraggable = !('ontouchstart' in document.documentElement);
var mapOptions = {
center: new google.maps.LatLng(latLong),
zoom: 12,
(...)
} else {
latLong = '<?php echo $latitude.",".$longitude;?>';
}
})
此外,如果要使用latLong = '<?php echo $latitude.",".$longitude;?>';
作为默认的latLong
,则需要在顶部声明它。那么当地理编码完成加载时,它无论如何都会被覆盖
编辑试试这个。
function getGeoLatLong(addresslat) {
geocoderlat.geocode( { 'address': addresslat}, checkresults)
}
function checkresults(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert("Outside function: " + results[0].geometry.location);
//declare the big chunk of code as a function and make a call to it.
} else {
alert("Outside function: <?php echo $latitude.','.$longitude;?>");
//declare the big chunk of code as a function and make a call to it.
}
}
getGeoLatLong(addresslat);
相关文章:
- 是否有任何理由不在不同的源文件中提供相同的模块
- 有没有理由编写一个IIFE,用前导'不是'-JavaScript中的运算符(!)
- JS变量“;消失”;没有理由,变得空虚
- 这个 getter-setter 闭包是否有充分的理由以这种方式在其内部对象中声明它是私有的
- 将异步 API 转换为同步 API.(我保证有一个很好的理由)
- 是否有充分的理由为所有 JavaScript 模块添加前缀“$”
- 有什么理由使用 !1 而不是 false
- 有没有理由不在 while 块声明中分配变量
- 有没有理由 math.round *1000 /1000 没有意义
- 有没有理由不在JavaScript中使用Strict模式
- 是否有充分的理由避免在JS中没有最终表达式的FOR循环
- 是否有理由将参数转换为局部变量
- 是否有充分的理由围绕requireJS模块定义包装一个额外的无限调用函数
- 是否有很好的理由不使用位操作符而使用parseInt ?
- 是否有理由在grails中使用JavaScript库插件,通过TagLibs公开给定的库,而不是直接使用它?
- NodeJS承诺vs回调:有理由不支持两者吗?
- 是否有理由在Javascript中使用单引号而不是双引号
- 是否有理由使用array.forEach()而不是for..在ES6中,两者都可以使用
- 是否有理由不使用document.getElementById的简写?
- 是否有理由在函数中创建一个局部$(this)变量?