如何克服javascript数组迭代跳过记录/秒

How to overcome javascript array iteration skipping record/s

本文关键字:迭代 记录 数组 javascript 何克服 克服      更新时间:2023-09-26

出现问题,在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:(基于更新的几个注释)

  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. 你应该在你的done方法中重置currentquestion = 0;