简体   繁体   English

Javascript SetInterval执行脚本,每个间隔13次

[英]Javascript SetInterval executing script 13 time every interval

So here is my script, I don't understand at all what is happening. 所以这是我的脚本,我完全不了解发生了什么。

var reloadTableInterval = setInterval(function(){
    console.log('ABC');
}, 120000);

When I do this, I notice that it writes 13 time the "ABC" in my console everytime the interval is completed. 当我这样做时,我注意到每次间隔完成时,它会在控制台中写入13倍的“ ABC”。 I'm wondering if I'm doing something wrong? 我想知道我做错了什么吗?

Note that this function is part of a AngularJS(1.3) controller. 请注意,此函数是AngularJS(1.3)控制器的一部分。

app.controller('unitCtrl', ['$scope', '$http', '$compile', function($scope, $http, $compile){
    //...
}]);

Also, I have 7 directive that uses my Controller, they are all built this way but with different name: 另外,我有7条指令使用我的控制器,它们都是以这种方式构建的,但名称不同:

app.directive('clientInfoModal', function () {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: '/reservations/template/ClientInfoModal.html',
        controller: 'unitCtrl'
    };
});

And they are loaded at the bottom of my HTML document (it's a single page small application) 它们被加载到我的HTML文档的底部(这是一个单页的小型应用程序)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controller/unitController.js"></script>
<script src="js/directive/saleModal.js"></script>
<script src="js/directive/reservationModal.js"></script>
<script src="js/directive/clientInfoModal.js"></script>
<script src="js/directive/unitsTable.js"></script>
<script src="js/directive/waitingListModal.js"></script>
<script src="js/directive/oneUnitWaitingListModal.js"></script>
<script src="js/directive/newNoteModal.js"></script>

Edit: Fixed answer based on OP's question comment thread 编辑:固定答案基于OP的问题注释线程

app.service('intervalCheckService', ['$interval', function($interval) {
  return {
    intervalRun: false,
    setInterval: function() {
      if (!intervalRun) {
        this.intervalRun = true;
        var reloadTableInterval = $interval(function() {
          console.log('ABC');
        }, 120000);
      }
    }
  }
}]);

Include that service in your app as a dependency. 将该服务作为依赖项包含在您的应用程序中。 Then inject it into your controller. 然后将其注入控制器。

app.controller('unitCtrl', ['$scope', '$http', '$compile', 'intervalCheckService', function($scope, $http, $compile, intervalCheckService) {
  intervalCheckService.setInterval();
  //...
}]);

The angular docs has a great example of a service as well if you need more examples: See Here 如果您需要更多示例,Angular文档也提供了很好的服务示例: 请参见此处

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

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