1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 处理异步问题

处理异步问题

时间:2019-08-17 20:47:59

相关推荐

处理异步问题

1、for循环中处理异步问题(调用查询数据库的耗时操作)

错误代码:(下面这个代码如果执行后,会出现查询出来的值赋值混乱的现象)

if ($scope.isOther(orderClass)) {getStartAndEndDateTime(data.object.packBeginDay, data.object.packEndDay, function (startDateTime, endDateTime) {for (var i in cpExamLabMasterList) {//for循环中查询数据库的操作cpExamLabMasterList[i].orderClass = orderClass;var id = getId();id = packOrderDetail.id + id;cpExamLabMasterList[i].startDateTime = startDateTime;cpExamLabMasterList[i].endDateTime = endDateTime;$scope.cpOrderTreatList.push(cpExamLabMasterList[i]);var orderDetail = getOrderDetailTreat(cpExamLabMasterList[i], id, packOrderDetail.id);orderDetail.performDeptList = [];/*处置|其他执行科室 查询数据库耗时操作*/if(cpExamLabMasterList[i].cpTreatItemList.length > 0){var successCallback = function (data) {orderDetail.performDeptList = angular.copy(data);//把查询出来的数据赋值给下标对应的对象};HrUtils.httpRequest($http, Path.getUri("api/treat-dict/perform-dept/"), successCallback , HrUtils.httpMethod.GET);}checkSelectAttr(packOrderDetail,orderDetail,i);$scope.cpOrderDetailList.push(orderDetail);}});}

正确代码:

if ($scope.isOther(orderClass)) {getStartAndEndDateTime(data.object.packBeginDay, data.object.packEndDay, function (startDateTime, endDateTime) {var aysncInfo = {index: 0, count: cpExamLabMasterList.length};refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);});}var refreshTreatWithPerformDept = function (orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo) {if (aysncInfo.index === aysncInfo.count) {return;}var cpExamLabMaster = cpExamLabMasterList[aysncInfo.index];cpExamLabMaster.orderClass = orderClass;var id = getId();id = packOrderDetail.id + id;cpExamLabMaster.startDateTime = startDateTime;cpExamLabMaster.endDateTime = endDateTime;$scope.cpOrderTreatList.push(cpExamLabMaster);var orderDetail = getOrderDetailTreat(cpExamLabMaster, id, packOrderDetail.id);checkSelectAttr(packOrderDetail, orderDetail, aysncInfo.index);$scope.cpOrderDetailList.push(orderDetail);aysncInfo.index++;if (orderDetail.itemCode && (orderClass === orderClassEnum.orderTreat || orderClass === orderClassEnum.orderNurse)) {var successCallback = function (data) {orderDetail.performDeptList = angular.copy(data);refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);};var errorCallback = function () {refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);};HrUtils.httpRequest($http, Path.getUri("api/treat-dict/perform-dept/"), successCallback, hrDialog, errorCallback, HrUtils.httpMethod.GET,);} else {refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);}};

---------------------------------------------------------------------------------------------------

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。