1

反復回数を渡す単純な js 関数があります。アラートで未定義と表示されるのが 1 回を超える場合、これは 1 回の繰り返しで機能します。test_iterations を ajax 関数内に保存するにはどうすればよいですか。

function _startTest(test_iterations) {
    $.ajax({
      url: '<?php echo $_SERVER['PHP_SELF']; ?>?start=1',
      success: function(data) {
        $('.results').append('<p>Test #' + test_number +' ' + data + ' ms</p>');
        $('.results').scrollTop(10000);

        test_results.push(data);
        alert("_startTest" + test_iterations);

        if(test_number >= test_iterations) {
            end_tests(test_iterations);
        }

        test_number++;
      }
    });

}
4

1 に答える 1

0

質問についてはよくわかりませんが、変数を関数の外に置いてみましたか? あなたはそれらをやっているようですtest_number

var iteration_count = 0;   // here
function _startTest(test_iterations) {
    iteration_count++;    // here
    $.ajax({
      url: '<?php echo $_SERVER['PHP_SELF']; ?>?start=1',
      success: function(data) {
        $('.results').append('<p>Test #' + test_number +' ' + data + ' ms</p>');
        $('.results').scrollTop(10000);

        test_results.push(data);
        alert("_startTest" + iteration_count); // use this value instead
        if(test_number >= test_iterations) {
            end_tests(test_iterations);
        }
        test_number++;    
      }
    });

}
于 2013-02-15T06:45:33.447 に答える