简体   繁体   English

Javascript 中非常奇怪的行为。 代码运行不正常

[英]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.在 if 语句中设置布尔值会在到达 if 语句之前设置。 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 .我认为这与控制台出现乱序有关,但我现在可以在 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
});

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.请注意,根据日志,我的“问题值”对于 shapeNotes[1] for combine[1] 设置为 false,但是当它在我的第一个 for 循环中达到该值时,它立即以某种方式设置为 true 并且内部的 j for 循环或控制台日志永远不会发生。 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.奇怪的是, transPlayed === false && noteAt >= transNoteAt条件中的布尔设置notesObj.trans.all.combined[i].played = true立即发生,同时该条件中的其他代码都不是执行,即使我设置了断点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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