1

jquery.eachループに.getが含まれています。ループ内のすべてが完了するまで待ち(各getは.completeで新しい要素を配列にプッシュします)、次にphpファイルにajax postリクエストを実行して、作成された配列を保存します。それぞれが終了するまでajaxリクエストを待機させる方法がわかりません。これまで、ajaxは非常に高速に起動します。したがって、それぞれの内部への取得は起動時にまだ終了していないため、配列はまだ空です。多分あなたは考えを持っています。

スクリプトは次のとおりです。

$('#ScanButton, .ScanButton').click(function() {

var array = ["http://www.xyz.com/bla/bla/summary.html",
             "http://www.xyz.com/blu/blu/summary.html",
            ];

dataArray = [];

$.each(array, function(n, val) { 


    $.get(val, function(res) { //get the html source of this website



      var data = {

      }

  }).complete(function() { 
        alert("complete"); 
        dataArray.push(data);
    });

});

    data = YAHOO.lang.JSON.stringify(dataArray);      

  $.ajax({
    async:           false,
    type:           'post',
    cache:          false,
    url:            'test.php',
    data:           {myJson:  data}
    });

  return false;

});

助けていただければ幸いです。ありがとうございました :)

4

1 に答える 1

1

Underscore.js には、この種の問題に対する洗練されたソリューションがあります

var renderNotes = _.after(notes.length, render);
_.each(notes, function(note) {
  note.asyncSave({success: renderNotes}); 
});
// renderNotes is run once, after all notes have saved.
于 2011-08-09T16:26:06.410 に答える