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

标签 javascript angularjs

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

我怎样才能做到这一点并同时调用 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);
      }

最佳答案

根据 the doc of $interval ,第三个参数“count”是

Number of times to repeat. If not set, or 0, will repeat indefinitely.

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

你需要做的是:

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

Here is a demo plunkr for better understanding

关于javascript - 如何在angularjs中将多个参数传递给$interval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55774988/

相关文章:

javascript - Owl Carousel 2 和 Featherlight.js – mouseDrag 导致打开灯箱

javascript - CSS/jQ/jS - Prev/Next 按钮不会堆叠在 img cycler 上

javascript - 仅针对 iPad 开发网站 - 强制所有链接在应用程序模式下打开

javascript - 在数组原型(prototype)中循环

javascript - Angular ng-max 绑定(bind)问题

javascript - Ajax : check email availability with PHP

node.js - 如何使用socket.io向指定客户端发送数据?与 Angular、NodeJs、Express

javascript - 定义自定义服务的语法

javascript - 在 $http 发布期间显示进度通知?

twitter-bootstrap - Angular 与 Bootstrap 弹出窗口。如何在生成的弹出框主体中应用绑定(bind)?