私はコール時にタイルでページを埋めるべき関数を書いています。タイルに関するデータは、リモートDBから取得されます(したがって、AJAXリクエスト)。私はコード内でjQuery 3.0も使用しています。ここで実行順序bahaviour
機能です:
function populateDashboard() {
var tileBlueprint = '<div class="dashboard_tile">\
<div class="dashboard_tile_content">\
<table class="tile_table">\
<tr class="tile_title">\
<td>$title</td>\
</tr>\
<tr class="tile_data">\
<td>$value</td>\
</tr>\
</table>\
</div>\
</div>';
$.ajax({
url: 'http://' + AppVar.ServerUrl + '/restapi/Dashboard_GetData',
type: 'POST',
data: JSON.stringify({
'SessionId': AppVar.SessionId
}),
dataType: 'json',
contentType: "application/json",
success: function (data) {
if (data.ResultCode === '0') {
//current tiles get wiped
$('.dashboard_tile').fadeOut(100, function() {
$(".tile_handler").empty();
console.log("1 - " + Date.now())
});
//new tiles are parsed and data is injected into the string which represents the tile
//tiles are saved into an array
var json = $.parseJSON(data.TileData);
var tileArr = [];
$.each(json.tiles, function (index, element) {
tile = tileBlueprint.replace("$title", $.i18n("dashboard-" + element.title));
tile = tile.replace("$value", element.value);
tileArr[index] = tile;
console.log("2 - " + Date.now())
});
//now I loop trough the previously created array to populate the page
$.each(tileArr, function (index, element) {
setTimeout(function() {
$(element).hide().appendTo(".tile_handler").fadeIn(1000);
}, 1000 * index); //delay made longer to see the effect better
console.log("3 - " + Date.now())
});
} else {
ons.notification.alert($.i18n('error-retriving_data_problem'));
return;
}
},
error: function (request, error) {
ons.notification.alert($.i18n('error-conn_error'));
return;
}
});
}
私はその部分が正常に動作しているように、このが注入なっているHTMLがrelevatではないと思います。
問題はは順不同でコールアウトを取得し、成功時に呼び出されますとの両方各ループをフェードアウトということです。私は、それぞれの実行を取得した時刻を記録しようとした、これは私が得るものです:
//first run
2 - 1469707068268 (6 times)
3 - 1469707068269 (6 times)
//second run
2 - 1469707181179 (2 times)
2 - 1469707181180 (3 times)
2 - 1469707181181
3 - 1469707181181
3 - 1469707181182 (4 times)
3 - 1469707181183
1 - 1469707181283
1 - 1469707181284 (2 times)
1 - 1469707181285 (2 times)
1 - 1469707181286
コメントとは6回だけを発射する必要がありますので、私は、6枚のタイルを表示しています一度。
はなぜは、最初に実行しないのですか?
なぜが6回実行されるのですか?編集:私はちょうど今自分自身を考え出しました。
が最後に実行されると、以前に作成されたタイルはすべて削除されないのはなぜですか?
もう1つの問題は、最初に6つのタイルを表示するが、2番目(以降)、5つのタイルしか表示しないことです(最初は欠落しています)。
誰でも私が何が起こっているのか、どのようにそのような大騒ぎを避けることができますか?
ありがとうございます。
私は 'JSON.stringify'を使いませんでしたが、.NETのバックエンドはデータを受け取っていませんでした。 '$ .each(data.TileData.tiles、function(index、element){'、コードがちょうど全く応答しないところ)バックエンドが変わっているかどうかわからない – rancor1223
申し訳ありません、あなたは'$ .each(data.TileData.tiles、function(index、element){'。については、バックエンドからのJSON文字列をプレースホルダ出力として解析していたので動作しませんでした。適切なJSONを送信すると、あなたの解決策は正しいです。 – rancor1223
@ rancor1223喜んで私は助けることができました! – MonkeyZeus