【发布时间】:2011-06-10 16:07:52
【问题描述】:
我有一个 javascript 函数,它进入一个循环并为每个循环发出一个异步 ajax 调用。我需要知道所有 ajax 调用何时返回并被处理,因为我想在那时更新 UI。
循环如下:
function sync_SyncLocalStorageToServer() {
if (helper_IsAppOnline()) {
$.log('sync_SyncLocalStorageToServer detects app is ONLINE');
// Post each cached entry to the server - this is the main sync function
for (var i = 0, len = localStorage.length; i < len; i++) {
var lskey = localStorage.key(i);
if (lskey.substr(0, 8) === 'response') {
$.log('Sync found response with key=' + lskey);
var postdata = localStorage.getItem(lskey); // Get the form data
$.log('Calling server with ls response:' + postdata);
var localkey = lskey;
$.ajax({
type: "POST",
dataType: 'json',
url: "/Profile/PostForm",
data: { jsonpost: postdata },
success: function (data) {
if (data.rc == "success") {
localStorage.removeItem(data.localStorageKey); // Remove the relevant localStorage entry
$.log('ServerSuccess:' + data.message + ',removed localStorageKey=' + data.localStorageKey);
} else {
$.log('ServerUnhappy:' + data.message + ',did not remove localStorageKey=' + data.localStorageKey);
}
}
, error: function (data) {
$.log('ERR:' + data);
}
});
}
}
}
else {
$.log('sync_SyncLocalStorageToServer detects app is OFFLINE');
}
}
当最后一个异步 ajax 调用最终从服务器返回时,我调用 UI 刷新函数的最简单方法是什么?
谢谢。
【问题讨论】:
标签: javascript jquery ajax asynchronous