0

私のページにはケースがあり、6つのウィジェット(つまりdiv)があり、それぞれが異なる間隔で1つのajax関数を呼び出しています(1つのdiv間隔は20秒、他の1つは30秒です)。サーバーから結果を取得した後、i対応するパネルを更新します。このシナリオを処理するための最良の方法はどれですか。このパネルごとに6つの異なるjavascriptタイマーを保持することをお勧めします(例:setInterval、setTimeoutなど)。そうでない場合は、処理するための最良の方法は何ですか。このシナリオ。

4

1 に答える 1

2

解決:

duration = 20;

(function getMoreData(divIndex) {
    console.log("Process First");

    // Get ajax data for first div
    $.ajax({
        url: url1,
        dataType: 'json',
        data: "",
        timeout: 5000,
        success: function(data) {
                dataFlag[divIndex]= true;

                // Process data...
                // Update "div 1"
            },
        error: function() {
                dataFlag[divIndex]= true;

                // Process error
            }
    });


    // Second div
    console.log("Process Second");


    // Get ajax data for second div
    $.ajax({
        url: url2,
        dataType: 'json',
        data: "",
        timeout: 5000,
        success: function(data) {
                dataFlag[divIndex]= true;

                // Process data...
                // Update "div 2"
            },
        error: function() {
                dataFlag[divIndex]= true;

                // Process error
            }
    });


    // Third div
    console.log("Process Third");


    // Get ajax data for second div
    $.ajax({
        url: url3,
        dataType: 'json',
        data: "",
        timeout: 5000,
        success: function(data) {
                dataFlag[divIndex]= true;

                // Process data...
                // Update "div 3"
            },
        error: function() {
                dataFlag[divIndex]= true;

                // Process error
            }
    });


    setTimeout(getMoreData, (duration * 1000));
})()
于 2013-01-04T06:18:12.173 に答える