简体   繁体   English

如何识别Angular JS中的特定间隔来销毁

[英]How to identify the specific Interval in angular js to destroy

Below directive is used for getting data continuously from server with default interval of 5 sec 以下指令用于从服务器连续获取数据,默认间隔为5秒

app.directive('livestatDataGrid', livestatDataGrid);

function livestatDataGrid($http, $interval, $window) {
    return {
        restrict: 'E',
        templateUrl: 'directives/livestatDataGrid/livestat-datagrid.html',
        scope: {
            viewName: '=viewName',
            gridOptions: '=',
            deleteItem: '=deleteItem'

        },
        link: function($scope) {
            var defaultSec = 5;
            $scope.timerObj = {};
            $scope.timerObj.selectedItem = defaultSec;
            $scope.timerObj.isDisplay = true;
            var timer = null;
            $scope.copyVal = defaultSec;


            $scope.getViewData = function(viewName) {
                console.log('Came here !! Directive ', viewName);
                $http.get('json/' + viewName + '.json').then(function success(response) {
                    $scope.metricHeader = response.data.metricHeader;
                    $scope.stats = response.data.stats;
                }, function error(error) {
                    console.log("Eooor ", error)
                });
            };


            $scope.getViewData($scope.viewName);

            // Timer code

            $scope.editTimer = function() {
                $scope.timerObj.isDisplay = false;
            };

            $scope.setTimer = function() {
                if ($scope.timerObj.selectedItem && isNaN($scope.timerObj.selectedItem)) {
                    $scope.timerObj.selectedItem = defaultSec;
                }
                if ($scope.copyVal != "" && $scope.copyVal == $scope.timerObj.selectedItem) {
                    $scope.timerObj.isDisplay = true;
                    return;
                }
                $scope.StopTimer();
                $scope.copyVal = angular.copy($scope.timerObj.selectedItem);
                $scope.timerObj.isDisplay = true;
                $scope.startTimer();
            };

            // starting the timer here once directive is initialized.Using `$interval`


            $scope.startTimer = function() {
                console.log("in timer " + $scope.timerObj.selectedItem);
                if ($scope.timerObj.selectedItem && $scope.viewName) {

                    //time interval for sending request to server
                    timer = $interval(function() {
                        if ($scope.viewName)
                            $scope.getViewData($scope.viewName);

                        $interval.cancel(this);
                        console.log(timer)
                    }, $scope.timerObj.selectedItem * 1000);

                }
            };
            $scope.startTimer();

            // Once the job is done, destroy the timer using `$interval.cancel`.

            $scope.StopTimer = function() {
                if (angular.isDefined(timer)) {
                    console.log("in stop timer");
                    $interval.cancel(timer);

                }
            };
            //timer code ends  her
        }


    }
}

Not clear what do you intend to ask/discuss. 不清楚您打算问/讨论什么。 Is this a self answer question? 这是一个自我回答的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM