AngularJS实现页面定时刷新,切换页面停止刷新

代码如下:

app.controller('businessCtrl', function($scope, $interval, $http) {
    $scope.shuaxin = function() {
        getTransCodes(i_pagenum, i_pagesize, $http, $scope);
    }

    getTransCodes(i_pagenum, i_pagesize, $http, $scope);

    var autoRefresh;
    //自动刷新
    autoRefresh = $interval($scope.shuaxin, 1000);
    //停止自动刷新
    $scope.stopAutoRefresh = function() {
        if(autoRefresh) {
            k = 0;
            $interval.cancel(autoRefresh);
            autoRefresh = null;
        }
    };

    //切换页面时停止自动刷新
    $scope.$on('$routeChangeStart', function(angularEvent, current, previous) {
        $scope.stopAutoRefresh();
    });
});

function getTransCodes(i_pagenum, i_pagesize, $http, $scope){
    ......
}

你可能感兴趣的:(angularjs,定时任务,函数)