我有這個問題與jQuery。我的jQuery中的一些函數返回「未捕獲的ReferenceError:curLocZip未定義」當我在另一個函數中調用它時。我有這個問題與jquery與一個功能沒有定義的指示,同時我定義的功能
下面是函數的定義:
function curLocZip(){
curZipCode = "";
if (navigator.geolocation) { //Checks if browser supports geolocation
navigator.geolocation.watchPosition(function (position) {
var geocoder = new google.maps.Geocoder();
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lng)
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
for (i = 0; i < results[0].address_components.length; i++) {
if (results[0].address_components[i].types[0] == 'postal_code'){
curZipCode = results[0].address_components[i].short_name;
break;
}
}
}
} else {
console.log("Geocoder failed due to: " + status);
}
});
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
我叫它在另一個功能,像這樣:
curLocZip();
我不知道爲什麼我收到表示錯誤curLocZip () 沒有定義。
代碼中的「curLocZip」定義在哪裏?它在哪裏被稱爲? – Joseph
a)您何時以及何時定義了功能b)何時何地稱呼它? – Danmoreng
我在'$(document).ready(function(){...})中定義了_curloczip()_(fna curLocZip);'和我用相同的方法調用了它。通過在_curloczip()_中爲全局變量設置一個默認值,然後調用該函數,我注意到該函數在調用時實際返回一個值,只是在API處理完成之前返回該值。我現在正在尋找如何在函數返回被調用之前完成API處理。 –