0
這裏我有兩個函數:updateTooltipContent
和distance
。用javascript調用函數
當我嘗試撥打distance(latt)
在updateTooltipContent
它不會返回任何值。我不明白爲什麼不
CODE:
function updateTooltipContent() {
var fullt = $(this).width();
var startt = $(this).position().left + 200;
var endt = fullt + startt;
var latt = $(this).attr("lat");
return "Spending: " + formatTime(fullt) +
"</br> Between:(" + formatTime(startt) + " and " +
formatTime(endt) + ") </br>" + distance(latt) + "km";
}
});
function distance(latt) {
var bigArray = nArray();
var dis = 0.00;
for (var x = 0; x < bigArray.length; x++) {
if (bigArray[x].lat == latt) {
dis = bigArray[x].DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION;
break; // no point doing anymore loops as we've found the answer
}
return dis;
}
}
此代碼工作很好,但是當我嘗試把一些代碼在功能上則不會工作: 工作的代碼,而作用距離()
function updateTooltipContent() {
var fullt = $(this).width();
var startt = $(this).position().left + 200;
var endt = fullt + startt;
var latt = $(this).attr("lat");
var bigArray = nArray();
var distance = 0.00;
for(var x = 0; x < bigArray.length; x++)
{
if(bigArray[x].lat == latt)
{
distance = bigArray[x].DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION;
break; // no point doing anymore loops as we've found the answer
}
}
return "Spending: "+formatTime(fullt) + "</br> Between:("+formatTime(startt) + " and " +formatTime(endt)+") </br>" + distance.toFixed(2) + "km";
}
});
*「出現一些錯誤」*我們是否必須猜測錯誤或者是否要告訴我們有關錯誤? [學習如何**調試** JavaScript](http://www.netmagazine.com/tutorials/javascript-debugging-beginners)。 –
是的fnction不返回任何值,距離(格)不返回任何值 –
是'bigArray.length'大於0?否則你的函數不會返回任何東西。 – putvande