简体   繁体   中英

Very strange behavior in Javascript. Code running out of order

I have trouble describing this since I cannot recreate any isolated instances. Setting a boolean inside an if statement gets set before it ever reaches the if statement. I thought this had to do with the console having the log appear out of order, but I can now see the result in the j for loop on occurring unless I remove 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
});

Just to include what my log shows, here it is:

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

Notice my "troubled value" is set to false for shapeNotes[1] for combined[1] according to the log, but when it reaches that in my first for loop, it is immediately set to true somehow and the j for loop inside or the console log never transpires. Oddly, the boolean setting notesObj.trans.all.combined[i].played = true inside the transPlayed === false && noteAt >= transNoteAt conditional takes place immediately, and at the same time none of the other code inside that conditional is performed, even if I set a breakpoint.

也许你应该把 ++i 改为 i++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM