0

FirefoxとSafariで発生する問題がありますが、Chromeでは正常に動作します。

このjavascript配列には、一度に1つずつ表示するクイズの質問が含まれています。ただし、すべてのレコードが繰り返されるわけではありません。私はコンソールを使用しました、そしてそれは間違いなくすべての質問をロードしています、しかしそれでも時々レコードをスキップします(通常最初のものだけ)。

編集:クイズでこの配列が機能することに気づきました。すべての質問IDが順番に並んでいます。つまり、配列内の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
4

1 に答える 1

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;、doneメソッドでリセットする必要があります。
于 2013-03-25T13:20:04.870 に答える