1

毎秒Ajaxリクエストを実行したいと思います。次のコードは完全に機能します。

    window.onload = function () {
        setTimeout(doStuff, 1000); //wait before continuing
    }

    function doStuff() {
        $.ajax({
            // ...
        });
        setTimeout(doStuff, 1000);
    };

しかし、現時点では、Fiddlerなどのツールを使用してリクエストをブロックすると、システムは新しいAjaxリクエストを送信し続けます。それらをキューに入れ、サーバーの応答後にのみ次のAjaxリクエストを送信したいと思います。どうやってやるの ?

4

5 に答える 5

2

成功のコールバック内で setTimeout を呼び出します。

function doStuff() {
    $.ajax({
        // ...
        success: function(result) {
            // ...
            setTimeout(doStuff, 1000);
        }
    });
}
于 2012-06-29T14:28:14.253 に答える
1

同様の問題に遭遇して解決したので、SOで答えを共有したいと思いました。

これが私がしたことです:

//グローバル変数:

var ajaxQue = [];   //array containing immediate que of ajax object request to be fired off
var ajaxCache = {}; //object holding ajax requests. 
var ajaxThreadActive = 0;   //var for indicating if the ajaxQue if currently firing off or not. 



//get the last (newest) ajax Request
function getLastRequest() {
    for (i in ajaxCache) {
        ajaxQue.push(ajaxCache[i]);
        delete ajaxCache[i];
        return; //aim to only get one request at a time inorder to catch more requests
    }
    //if no items in the cache exist, will come here
    ajaxThreadActive = 0; //ajax queue is now empty
}

//put an ajax request in an obj with a specific id so that if a newer ajax request is created before the ajax requests are finished sending, it will replace the old one
function queRequest(ajaxObj, id) {
    if (arguments.length != 2) {    //id argument is optional
        id = uuid++;    //create unique id as default
    }
    if (id in ajaxCache) {
        console.log('duplicate request');   
    }
    ajaxCache[id] = ajaxObj;
    if (ajaxThreadActive == 0) {
        ajaxThreadActive = 1;
        getLastRequest();   //retrieve most 'updated' ajax request
        fireOffAjaxQue();   
    } else {
        return 'ajax thread is running';
    }
}


//fire off the ajax queue
function fireOffAjaxQue () {
    if ((ajaxQue.length > 0) && ajaxThreadActive == 1) {    //an if check on the thread active incase I want to close this from another place in code
        $.ajax(ajaxQue[0]).always( function () {
            setTimeout(function () {
                getLastRequest();   //retrieve most 'updated' ajax request
                fireOffAjaxQue();
            }, 50); //fire off another ajax request since this one has been completed.
        });
        ajaxQue.shift();    //perform this immediately after the ajax request is sent, will execute before the .always() function
    } 
}

jQuery で通常行うこととは異なり、使い方は簡単です。

$.ajax({url: 'someplace.php',
        data: dataVar,
        success: function(data) {...});

これを変更します:

//create ajax object
var ajaxObj = {url: 'someplace.php',
               data: dataVar,
               success: function (data) {...}};
 //send ajax object to que

次に、これを使用して Que に送信します。

 queRequest(ajaxObj);  //send to que without an id since this is a unique request
 // *******OR********
 queRequest(ajaxObj, id);   //send to the que with an id IF this is to replace any 'older' requests of the same id

最新のajax リクエストを保持するために AjaxCache を含めました。つまり、ユーザーのキーストロークで繰り返し要求を送信する関数がある場合、最新の最新の要求 (フォーム情報など) のみを送信する必要がある場合があります。このマネージャーが作成される方法では、関連付けられた ID (オプション) を持つ要求を処理して、同じ ID で要求を上書きすることにより、新しい要求が古い要求を置き換えることができるようにします。

//for an example, I use it like this in my webpage
queRequest(ajaxObj, 'table1Data'); 

ここで、キューがまだ発生している場合に備えて、作成したリクエスト'table1Data'は ajaxObj を id で上書きする'table1Data'だけで、最小限の量の Ajax リクエストのみを送信します。

于 2013-02-12T08:51:12.100 に答える
0

async を使用して ajax リクエストが最初に完了するまで待機させる

jQuery.ajax({
    url: "query.php?Time="+myTimestamp(),
    async: false,
    success: function(data){
        jQuery.ajax({
            url: "query.php?Time="+myTimestamp(), 
            async: false,
            success: function(data){

            }
        });
    }
});
于 2012-06-29T14:26:59.437 に答える
0

私は使用しました:http://code.google.com/p/jquery-ajaxq/本番アプリケーション用。

実行中の ajax リクエストを中止するようにコードをカスタマイズしました。

  • ナビゲーション Ajax (画面への移動) - 中止を使用しました。

  • データ送信 - 使用済みキュー。異なる名前の異なるキューを持つことができます。

于 2012-06-29T14:27:44.083 に答える
0

リクエストの成功ハンドラーで次のリクエストのトリガーを前に移動するだけです。

function doStuff() {
    $.ajax({
        // ...
        success: function( data ) {
           // your other code
           setTimeout(doStuff, 1000);
        }
    });  
};
于 2012-06-29T14:29:48.297 に答える