[英]Very strange behavior in Javascript. Code running out of order
我無法描述這一點,因為我無法重新創建任何孤立的實例。 在 if 語句中設置布爾值會在到達 if 語句之前設置。 我認為這與控制台出現亂序有關,但我現在可以在 j for 循環中看到結果,除非我刪除notesObj.trans.all.combined[i].played = true
。
if(shapesNotes.length === 2){
if(shapesNotes[1].trans.all.combined.length == 2){
console.log("troubled value = " + shapesNotes[1].trans.all.combined[1].played)
}
}
function setTranspositionsForAllNotes(notesObj,debugIndex){
for(var i=0; i<notesObj.trans.all.combined.length; ++i){
var transNoteAt = notesObj.trans.all.combined[i].noteAt
var transPlayed = notesObj.trans.all.combined[i].played
console.log("debugIndex = " + debugIndex + " i = " + i)
console.log("notesObj.trans.all.combined.length = " +notesObj.trans.all.combined.length)
console.log("notesObj.trans.all.combined["+i+"].played = " + notesObj.trans.all.combined[i].played)
console.log("shapesNotes["+debugIndex+"].trans.all.combined["+i+"].played = " + shapesNotes[debugIndex].trans.all.combined[i].played)
console.log("noteAt = " + noteAt + " transNoteAt = " + transNoteAt)
if(transPlayed === false &&
noteAt >= transNoteAt){
var transInterval = notesObj.trans.all.combined[i].interval
for(var j=0; j<notesObj.trans.array.length; ++j){
notesObj.trans.array[j] += transInterval
notesObj.trans.freqsPlusTrans[j] = getFrequencyPlusTranspositionForFreqIndex(notesObj,j)
}
console.log("transInterval = " + transInterval + " for " + debugIndex + " i = " + i)
notesObj.trans.all.combined[i].played = true
}
}
}
var debugIndex = 0;
shapesNotes.forEach(function(shapeNotes) {
setTranspositionsForAllNotes(shapeNotes,debugIndex)
debugIndex+=1
});
只是為了包括我的日志顯示的內容,這里是:
1980 troubled value = false
1981 debugIndex = 0 i = 0
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[0].played = false
1984 shapesNotes[0].trans.all.combined[0].played = false
1986 noteAt = 0.008333333333333333 transNoteAt = 0
1998 transInterval = 0 for 0 i = 0
1981 debugIndex = 0 i = 1
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[1].played = false
1984 shapesNotes[0].trans.all.combined[1].played = false
1986 noteAt = 0.008333333333333333 transNoteAt = 0
1998 transInterval = 2 for 0 i = 1
1981 debugIndex = 1 i = 0
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[0].played = false
1984 shapesNotes[1].trans.all.combined[0].played = false
1986 noteAt = 0.008333333333333333 transNoteAt = 0
1998 transInterval = 0 for 1 i = 0
1981 debugIndex = 1 i = 1
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[1].played = true
1984 shapesNotes[1].trans.all.combined[1].played = true
1986 noteAt = 0.008333333333333333 transNoteAt = 0
請注意,根據日志,我的“問題值”對於 shapeNotes[1] for combine[1] 設置為 false,但是當它在我的第一個 for 循環中達到該值時,它立即以某種方式設置為 true 並且內部的 j for 循環或控制台日志永遠不會發生。 奇怪的是, transPlayed === false && noteAt >= transNoteAt
條件中的布爾設置notesObj.trans.all.combined[i].played = true
立即發生,同時該條件中的其他代碼都不是執行,即使我設置了斷點。
也許你應該把 ++i 改為 i++
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.