angularjs代码refine,还是老一套

有段定时刷新的代码是这么写的:

       var is_getting = false;
        var refresh_interval = 2000;
        var get_encoder_channels = function() {
            if (is_getting) {
                return;
            }

            if ($scope.last_refresh_time) {
                $scope.left_time = (refresh_interval + $scope.last_refresh_time - +new Date())/1000;
                if ($scope.left_time >= 0) {
                    return;
                }
                $scope.left_time = 0;
            }

            is_getting = true;
            Encoder_integrated_service.summary(function(data) {
                is_getting = false;
                $scope.last_refresh_time = +new Date();
                $scope.summary = data;
            });
        };

        var stop;
        $scope.refresh = function() {
            if(angular.isDefined(stop)) return;

            stop = $interval(get_encoder_channels, 100);
            get_encoder_channels();
        };

        $scope.stop_refresh = function() {
            if (angular.isDefined(stop)) {
                $interval.cancel(stop);
                stop = undefined;
            }
        };

        $scope.$on('$destroy', function() {
            // Make sure that the interval is destroyed too
            $scope.stop_refresh();
        });

        $scope.refresh();

改成这样就好:

        async_refresh2.refresh_change(function(){
            Encoder_integrated_service.summary(function(data) {
                $scope.summary = data;
                async_refresh2.request();
            }, function(){
                async_refresh2.request();
            });
        }, 2000);

        async_refresh2.request(0);

Winlin 2014.6

你可能感兴趣的:(angularjs代码refine,还是老一套)