루프를 입력하고 각 루프에 대해 비동기식 아약스 호출을 발행하는 javascript 함수가 있습니다. 그 시점에서 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');
}
}
맨 마지막 비동기 아약스 호출이 결국 서버에서 반환 할 때 내 UI 새로 고침 함수를 호출하는 나를 위해 가장 쉬운 방법은 무엇입니까?
감사합니다.
그냥 함수의 맨 위에 var를 만들고 조정할 수 있습니까? 이런 식으로 가능한 범위 지정 또는 스레딩 문제가 있습니까? 매우 감사합니다. – Journeyman
@Journeyman이 (가) 내 답변을 편집했습니다. – Matt