如何在angularjs中将多个参数传递给$ interval

时间:2019-04-20 15:31:43

标签: javascript angularjs

每次调用$scope.iterationCount时,我都尝试更新$interval

我该如何做,并同时致电vm.life.next。在每次$scope.iterationCount调用之后,$interval应该增加1。


function GameController($interval, board, life, $scope){
      var vm = this;

      $scope.time= "1000";
      vm.thumbs = [];
      vm.reset = reset;
      vm.load = load;
      vm.togglePlay = togglePlay;
      vm.save = save;
      $scope.iteration=10;
      $scope.userSelect = '\u25CF';
      $scope.w=15;
      $scope.h=15;
      $scope.gridColor = '#ffffff';
      $scope.iterationCount= 0;
      function togglePlay(){
        if(!vm.isStarted && vm.timer){ 
          $interval.cancel(vm.timer);
          vm.isStarted = false;
          return;
        }
        vm.isStarted = true;
        vm.timer = $interval(vm.life.next, $scope.time, $scope.iteration);
      }

1 个答案:

答案 0 :(得分:1)

根据the doc of $interval,第三个参数“计数”为

  

重复次数。如果未设置,则为0,将无限期重复。<​​/ p>

因此,它不会递增,而是在到达该计数器后停止执行$interval

您需要做的是:

vm.timer = $interval(function(){
  vm.life.next();
  $scope.iteration++;
}, $scope.time);

Here is a demo plunkr for better understanding