zoukankan      html  css  js  c++  java
  • 处理异步问题

    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);
        }
    };

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

  • 相关阅读:
    CentOS上svn checkout时报错SSL handshake failed: SSL error: Key usage violation in certificate has been det
    SnmpTools配置
    Django下载文件
    Cacti的使用
    RRDtool 安装和使用
    SNMP 安装及使用
    Openstack CentOS6.5 ALL IN ONE 安装
    mysqladmin -u root password
    VS2013打包程序步骤
    python中使用rabbitmq消息中间件
  • 原文地址:https://www.cnblogs.com/ms-grf/p/7249833.html
Copyright © 2011-2022 走看看