まず、$.ajax
呼び出しがネストされていることを知っておく必要があります。最初のものは を出力し、table
それが完了すると、テーブルで 2 番目$.ajax
を実行each
<tr>
しています。どちらも使用しますasync: true
。
ビュー(テーブル)を最終ユーザーに提供したいので、これを行っています.3<td>
は他のURLからデータをロードする必要があります。この説明の後:
$.ajax({
// ...
complete: function() {
$('table tbody tr.product').each(function(i) {
let line = $(this);
...
// set variable
const data = { ...
};
// data values are from prev varaibles
$.ajax({
type: 'POST',
url: URIPOS,
data: data,
async: true,
beforeSend: function() {
tdMostSales.html('<i class="fa fa-spinner fa-spin fa-1x"></i>');
tdSubscriptions.html('<i class="fa fa-spinner fa-spin fa-1x"></i>');
tdGeneral.html('<i class="fa fa-spinner fa-spin fa-1x"></i>');
},
success: function(res) {
const resp = JSON.parse(res);
const {
general,
prevision
} = resp;
const {
total,
notified
} = resp.subscription;
tdMostSales.attr('data-value', prevision)
tdMostSales.html(`${prevision}`);
tdSubscriptions.html(`${notified}/${total}`);
tdGeneral.html(`${general}`);
},
complete: function() {
tdMostSales.blur();
}
});
if (i + 1 === $('table tbody tr.product').length) {
console.log('finished')
var options = {
valueNames: [
'name',
'sku',
'brand',
'match',
'profit',
'profit-rating',
'sell-rating',
{
name: 'pvp',
attr: 'data-value'
},
{
name: 'demand-forecast',
attr: 'data-value'
}
],
listClass: 'product-list-body'
//pagination : true,
//page: 20
};
var orderedList = new List('generator', options);
return;
}
});
}
});
しかし、これで、がまだ終了していないconsole.log('finished')
ときにを返し、$.ajax
まだいくつかの に取り組んでい<tr>
ます。それぞれが終了したときのコールバックかもしれませんが、どこでこれを行うか、どのように行うのですか?
ajax
これは重複した投稿ではないと思います...異なるものを作成してから取得したくないのでresponse
、1つの要求を作成する必要があり、それが終了したら次の要求を作成し、この2番目は各trを反復する必要がありますそれぞれに対してリクエストを行いますが、非同期であり、それが完了すると (さらにリクエストを行う必要はありません) 、すべてが によって反復されたreturn
後の応答です。tr
ajax