我在我的功能有下面這樣循環奇怪的Javascript功能迴路行爲
我看到它遍歷如果x < 10,但如果它是大於10失敗,即使Ÿ< x滿足的條件
function insert(x,y) {
for (var shift = y; shift < x; shift++) {
//get into loop for insert (9,2) but not for insert(10,2)
}
}
這是實際的功能,我想使用可視化場景rapheal.js它適用於insertBeforeTo前插入(9,2),但是當我嘗試insertBeforeTo(10,2),它不進入循環。
function insertBeforeTo(whichElementI, insertBeforeI) {
var twhichElement = blocks[whichElementI];
blocks[whichElementI].animate({ x: blocks[insertBeforeI].attr('x') }, 1000, ">");
var shiftplusone = insertBeforeI;
for (var shift = insertBeforeI; shift < whichElementI; shift++) {
++shiftplusone;
blocks[shift].animate({ x: blocks[shiftplusone].attr('x') }, 1000, ">");// check value actually changes
}
}
向下投票者:介意解釋?
發現問題:在調試時,我看到了''中的哪個ElementI和insertBeforeI值。所以我認爲它把它當作一個字符串,正如nnnnn和paxdiablo正確指出的那樣,它需要字符串而不是int,所以它適用於哪個ElementI = 9和insertBeforeI = 2,而不是whereElementI = 10,insertBeforeI = 2。
所以我使用+ whichElementI,+ insertBeforeI修正了這個問題。
感謝
當您輸入10,2作爲參數時會發生什麼?它根本不執行嗎? –
請多發一些代碼?試試console.log(shift);看看循環內部發生了什麼。 – Meryovi
在這兩種情況下都適用於我:http://jsfiddle.net/rxDXG/2/ –