javascript - Wait for AJAX to finish before proceeding with the loop? -
Why is it that whenever I put an AJAX inside the loop, it does not synchronize well? Why this calls Therefore, this code has different ways to work with the asynchronous concept. If you want only one asynchronous ajax call in flight at a time, then you have to reconstruct it and should not use the simple
function addToUserGroupList () {_root.qDomId ('js-assignGroupArrBtn'). Disabled = true (var i = 0; i & lt; selectedIds .length; i ++) {$ .ajax ({type: "POST", url: 'groupManage.ashx', data type: 'text', Data: 'type = get input and group id =' + selected EGS [i], success: function (result) {if (result! = '') {This.groupName = result.split ('& amp;') [0 ]; this.groupNotes = result.split ('& amp;') [2]; userGroupList.push ({'UID': PARASANT (Selected EIDS [I]), 'name': group.name, 'adminStr': this.groupNotes}); _root.userListObj.gourpInst.gourpTB (userGroupList);}}, error: function (XMLHttpRequest, status, error reporting) {warning ('Adding to user group Failed. ")}}}); } _root.qDomId ('js-assignGroupArrBtn'). Disabled = false; SelectedIds = []; }
selectedIds = []; Before the first Ajax query? Is it possible to eliminate AJAX questions before taking action for [Code> selectedIds = [] [[/ />>]? Because it fixes the array before ending the stuff First of all, you really need to understand how an Ajax call is for asynchronous (this is for Ajax in "A"). This means that
$ Calling Ajax () only starts AJAX calls (this sends the request to the server) and your rest code is happily running Sometimes, after the rest of your code, success or error callback handler Is called when the response comes back from the server. It is not sequential programming and should be contacted differently. # 1 thing means that whichever you want to be successful or error handler after call or call from there, after AJAX call. The code, immediately after the AJAX call, will run long before the completion of AJAX calls.
loop for it. Instead, you can create an index variable and end In the function, the index can increase and close the next instance. One way to code it is:
function addToUserGroupList () {_root.qDomId ('js-assignGroupArrBtn'). Disabled = true var i = 0; Function next () (if (i & lt; selectIds.length) {$ .ajax ({type: "POST", url: 'groupManage.ashx', data type: 'text', data: 'type = getInfo & amp; ; GroupId = '+ SelectIds [i], success: work (results) {i ++; if (result! =' ') {This.groupName = result.split (' & amp; ') [0]; this GroupNotes = result.split ('& amp;') [2]; User Group List. Push ({'UID': ParsInt (Selected EIDS [I]), 'Name': This GPNN, 'Administrator': this. GroupNotes}); _root.userListObj.gourpInst.gourpTB (UserGroupList);} Next ();}, Error: Function (XMLHttpRequest, Status, ErrorPrint) {Warning ('Add to user group Failed. ');}});} Else {// Last one did _root.qDomId (' js-assignGroupArrBtn '). Disabled = false; SelectedIds = [];}} // Next kick before one () ;}
Comments
Post a Comment