如何克服javascript数组迭代跳过记录/秒
How to overcome javascript array iteration skipping record/s
出现问题,在Firefox和Safari中出现,但在Chrome中可以正常工作
这个javascript数组包含测验问题,我一次显示一个。然而,有时并不是所有的记录都被迭代。我使用了控制台,它确实加载了所有问题,但有时仍然跳过一条记录(通常只有第一条)。
EDIT:我注意到在这个数组的测试中,所有的问题都是按顺序排列的,即数组中的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;
}
来自db的示例代码,
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
continue
语句导致循环跳过部分执行。试着调试一下,看看代码逻辑哪里有问题。
for(i in questions) {
if(i<=currentquestion) {
console.log('Skipping question: ' + i); // add debugging
continue;
}
...
EDIT:(基于更新的几个注释)
最好使用传统的for循环遍历数组:
for (var i = 0, len = questions.length; i < len; i++) {
但是外部循环不需要事件
如果初始化
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 }
看起来你的代码依赖于顺序,所以你可以排序
.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; }); ...
- 您可以使用
jquery.getJSON
或$.ajax( { success ....})
你应该在你的done方法中重置
currentquestion = 0;
。相关文章:
- 如何在javascript中迭代数字列表
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 如何迭代Array.prototype函数
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 在ejs-partial中对JSON对象进行迭代
- 如何在DataTables 2.1中迭代对象数组
- 使用递归属性迭代保留属性结构
- 正在停止.在jquery中的特定时间间隔内,每次迭代的每次执行
- 如果30秒未单击,请应用CSS一次,将其删除,然后重新迭代
- 主干-从模板中迭代的集合中获取特定的模型
- 创建一个方法,通过一个窗口进行迭代并获取Titanium中的所有控件
- 什么's是在javascript中迭代项的最佳方式
- 循环根据长度精确迭代,但每次迭代中插入相同的记录
- 在Ember.js Ember数据记录数组上迭代
- Jquery数组重复记录内的tr迭代
- 将EJS中的列表迭代限制为前50条记录
- 如何克服javascript数组迭代跳过记录/秒
- 如何迭代地创建记录集合并一次呈现所有记录
- Extjs -在缓冲存储中迭代显示记录的最佳方式
- 为什么在循环regexObj.test(str)时记录每次迭代的不同结果