我似乎在某種程度上是失去了一個可變的IM設置的值...的Javascript - 可變失去它的價值和「成爲」未定義
是林試圖做的不是那麼重要了,所以我已經設置了一(很好的註釋)的jsfiddle告訴你得到什麼IM。代碼如下。
如果任何人都可以看到最新的任何幫助會表示讚賞:)
見的jsfiddle>http://jsfiddle.net/qNWuV/4/ <建議您到這裏看看
var habs = ["417,77", "410,363", "388,433", "262,435", "262,210", "391,101", "384,183", "61,114", "331,171", "164,433", "361,248", "302,329", "154,307", "410,350", "173,298", "308,429"]; //just an array of co-ords for another part of my app. Only the .length is used below.
//############################
// NOTE: as this problem depends on random numbers you MAY not see it. If "undefined" is ANYWHERE in the Result, the problem is occurring, otherwise re-run the code.
//############################
function link_habs(habs) {
var test2 = '';
var hab_length = habs.length;
for (var e in habs) {
var hab_link_1 = get_link(hab_length, e + ',');
var hab_link_2 = get_link(hab_length, e + ',' + hab_link_1);
document.write('<br /><br />each1: ' + hab_link_1); //Variable lost?
document.write('<br />each2: ' + hab_link_2 + '<br />'); //Variable lost?
test2 += e + ':' + hab_link_1 + ',' + hab_link_2 + '<br />';
}
document.write('<br /><br /><br />' + test2);
}
function get_link(count, not) {
var nots = not.split(',');
for (var i in nots) { nots[i] = parseInt(nots[i], 10); }
var hab_link = Math.floor(Math.random() * count);
if (nots.indexOf(hab_link) === -1) {
document.write('<br />returned: ' + hab_link); //Variable is intact HERE
return hab_link;
} else {
get_link(count, not);
}
}
link_habs(habs);
乾杯
查理
可以在'get_link'擺脫遞歸。從if語句中刪除else子句,並用'while(1){...}'包裝該方法的主體。 –
您已接受答案的事實可以解決此問題。沒有必要在標題中添加它。 –
我真的不明白爲什麼它很重要,但好吧,我不會再做這件事...... –