-6ren">
gpt4 book ai didi

javascript全局变量问题

转载 作者:行者123 更新时间:2023-11-30 07:29:53 25 4
gpt4 key购买 nike

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Streetview Example</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=<?=APIKEY?>"
type="text/javascript"></script>
<script type="text/javascript">

var myPano;
var newPoint;

function initialize() {
var fenwayPark = new GLatLng(42.345573,-71.098326);
var address = "1600 Amphitheatre Parkway, Mountain View, CA, USA";

var geocoder = new GClientGeocoder();
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
newPoint = point;
alert("inside of function: " + newPoint);
}
});
alert("outside of function: " + newPoint);
panoramaOptions = { latlng:fenwayPark };
myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
GEvent.addListener(myPano, "error", handleNoFlash);
}

function handleNoFlash(errorCode) {
if (errorCode == FLASH_UNAVAILABLE) {
alert("Error: Flash doesn't appear to be supported by your browser");
return;
}
}
</script>

当我运行这段代码时,alert("outside of function: "+ newPoint);没有得到任何值,但在函数 alert("inside of function: "+ newPoint);它得到了。

完整代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Streetview Example</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAgVzm2syhpm0A5rAKFMg3FBS-DTtkk0JB-Y_gKL-3MRZwBHch9RSjWJj17-fEEecjCvYeo1i7w_1yPw"
type="text/javascript"></script>
<script type="text/javascript">

var myPano;

function initialize() {

var geocoder = new GClientGeocoder();

var address = "1600 Amphitheatre Parkway, Mountain View, CA, USA";
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
panoramaOptions = { latlng:point };
myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
GEvent.addListener(myPano, "error", handleNoFlash);
}
});
}

function handleNoFlash(errorCode) {
if (errorCode == FLASH_UNAVAILABLE) {
alert("Error: Flash doesn't appear to be supported by your browser");
return;
}
}
</script>

我想在打开页面时显示地址:1600 Amphitheatre Parkway, Mountain View, CA, USA 的街景

最佳答案

内部函数是一个回调函数。意思是,它会等到 geocode api 调用在执行之前完成运行。这称为异步执行。您会注意到外部警报在内部警报之前触发,即使内部警报写入得更早。

任何需要有效 newPoint 值的代码都需要从回调函数中执行,就像内部警报一样。

关于javascript全局变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1134837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com