有一個問題,這發生在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
嗨 - currentquestion'初始化在哪裏? – suspectus 2013-03-25 13:05:23
作爲一個全局變量。 var currentquestion = 0; – user2177629 2013-03-25 13:09:38
如果我== 0,它會跳過當前的問題== 0 – jermel 2013-03-25 13:11:50