javascript - jQuery Ajax Race Condition best practice -
in button.click(function(){});
have following code:
var userid = $("#user-permission-edit").val(); var peid = $("#user-permission-entity-list").val(); var newparentpeid = $("#new-parent-pe").val(); var newpename = $("#pe-name-add").val(); $.ajax({ type: "post", url: 'addnewpe', data: { 'targetuserid': userid, 'targetpeid': peid, 'newpeparentid': newparentpeid, 'newpename': newpename}, success: function (data) { var dataobj = jquery.parsejson(data); console.log(dataobj); if (dataobj.status == "success") { alert("permissions have been updated."); } //update user pes combo } });
however, concerned of possible race condition of variables not getting values in time ajax call.
would safer?
var userid = $("#user-permission-edit").val(); var peid = $("#user-permission-entity-list").val(); var newparentpeid = $("#new-parent-pe").val(); var newpename = $("#pe-name-add").val(); $.when(function() { userid = $("#user-permission-edit").val(); peid = $("#user-permission-entity-list").val(); newparentpeid = $("#new-parent-pe").val(); newpename = $("#pe-name-add").val(); }).done(function() { $.ajax({ type: "post", url: 'addnewpe', data: { 'targetuserid': userid, 'targetpeid': peid, 'newpeparentid': newparentpeid, 'newpename': newpename}, success: function (data) { var dataobj = jquery.parsejson(data); console.log(dataobj); if (dataobj.status == "success") { alert("permissions have been updated."); } //update user pes combo } }); });
is necessary?
the $('....').val()
method synchronous call - script execution won't continue past line until have received value of element.
Comments
Post a Comment