2013-03-25 79 views
0

有一個問題,這發生在Firefox和Safari,但在Chrome中,它工作正常。如何克服JavaScript數組迭代跳過記錄/ s

此javascript數組包含測驗問題,我一次顯示一個問題。但是,有時並不是所有的記錄都會迭代。我使用了控制檯,它肯定會加載所有問題,但有時會跳過一個記錄(通常只有第一個)。

編輯:我注意到,在測試這個數組的工作原理時,所有的問題都是按順序排列的,即數組中的290,291,293。但在一個不工作的例子中,測驗ID是這樣的順序,286,285,287,288和285是跳過的,這可能是問題的一部分。

這裏是我的Javascript數組代碼,請幫我解決這個問題。從分貝

var currentquestion; 

     jQuery.ajax({ 
        url:"quizajax.php", 


     dataType: "json", 
       data: { 
        quizidvalue: <?=$thisquizid?> 
       }, 
     }).done(function(data) { 
       questions = data; 
       for(i in data){ 
        console.log(data[i]); 

       } 
      }); 

function nextQuestion(){ 
     for(i in questions) { 
      if(i<=currentquestion) 
       continue; 

      currentquestion = i; 

      for(y in questions[i]) { 
       console.log("CurrentA: "+ currentquestion); 
       console.log("I: " + i); 
       console.log(questions[i][y].answerid); 
      } 
      console.log("CurrentQ: "+ currentquestion); 
      console.log("I: " + i); 
      console.log(questions[i]); 
      questionVariables(); 
      break; 
     } 

實施例的代碼,

questionid | quizid | questiontype | qdescription |   qfilelocation   | noofanswers | answertype 
------------+--------+--------------+------------------+------------------------------------+-------------+------------ 
     285 |  55 | text   | 6 answer text | null        |   6 | text 
     287 |  55 | text   | 4ans text q  | null        |   4 | text 
     289 |  55 | text   | 2 answers text q | null        |   2 | text 
     286 |  55 | text   | 5 answer text q | null        |   5 | text 
     288 |  55 | text   | 3 answer text q | null        |   3 | text 
     290 |  55 | image  | image q and a | image/55/712013a88298585d415c.jpeg |   4 | image 
     291 |  55 | video  | video q and a | video/55/8efa10195f0c20d1254f.mp4 |   4 | video 
+0

嗨 - currentquestion'初始化在哪裏? – suspectus 2013-03-25 13:05:23

+0

作爲一個全局變量。 var currentquestion = 0; – user2177629 2013-03-25 13:09:38

+0

如果我== 0,它會跳過當前的問題== 0 – jermel 2013-03-25 13:11:50

回答

1

continue語句導致循環跳過其執行的一部分。 嘗試調試,以查看代碼邏輯在哪裏有錯誤。

for(i in questions) { 
     if(i<=currentquestion) { 
      console.log('Skipping question: ' + i); // add debugging 
      continue; 
     } 
     ... 

編輯:(基於更新了若干意見)

  1. 它最好是通過使用傳統的for循環數組遍歷:

    for (var i = 0, len = questions.length; i < len; i++) { 
    

    所需的外環心不是事件,但

  2. 如果您希望初始化麗澤var currentquestion = 0;外循環可更換

    function nextQuestion(){ 
    
        for(y in questions[currentquestion]) { 
         console.log("CurrentA: "+ currentquestion); 
         console.log(questions[currentquestion][y].answerid); 
        } 
        console.log("CurrentQ: "+ currentquestion); 
        console.log(questions[currentquestion]); 
        questionVariables(); 
        currentquestion++; //update here 
    } 
    
  3. 它看起來像你的代碼依賴於訂單,所以你可以排序

    .done(function(data) { 
        questions = data; 
        questions.sort(function(q1,q2){ 
    
        //assuming questionid is the correct property 
         if (q1.questionid < q2.questionid) { 
          return -1; 
         } else if (q1.questionid > q2.questionid) { 
          return 1; 
         } 
         return 0; 
        }); 
        ... 
    
  4. 您也可以使用jquery.getJSON$.ajax({ success ....})
  5. 你或許應該重新設置currentquestion = 0;在你完成的方法。
+0

在使用javascript控制檯時發現了一些新的信息,它在我的原始文章的編輯部分。感謝您的幫助到目前爲止 – user2177629 2013-03-25 13:33:11

+0

我設法讓它工作,並且您的解決方案得到了幫助,謝謝 – user2177629 2013-03-25 22:41:13