每次调用 $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);
关于javascript - 如何在angularjs中将多个参数传递给$interval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55774988/