AngularJS$间隔应动态处于-/减少状态

AngularJS$间隔应动态处于-/减少状态,angularjs,intervals,Angularjs,Intervals,我只想使用$interval(anyFunction(){},1000); 但1000的值也应该是可变的。 如果我通过定义一个变量来改变它,那么间隔在视图上不会改变 有人能举个例子说明如何更新$interval的“速度”吗 多谢各位 以防万一: 我的控制器: $scope.food = 0; var stop; var farmInterval = 1000; $scope.startFarming = function () { console.log('farming start

我只想使用$interval(anyFunction(){},1000); 但1000的值也应该是可变的。 如果我通过定义一个变量来改变它,那么间隔在视图上不会改变

有人能举个例子说明如何更新$interval的“速度”吗

多谢各位

以防万一:

我的控制器:

$scope.food = 0;

var stop;
var farmInterval = 1000;

$scope.startFarming = function () {
    console.log('farming started...');
    if (angular.isDefined(stop)) return;

    stop = $interval(function () {
        $scope.food += 1;
    }, farmInterval); // <-- this value 'farmInterval' should be variable
}

$scope.stopFarming = function () {
    if (angular.isDefined(stop)) {
        $interval.cancel(stop);
        stop = undefined;
    }
}

$scope.increaseFarmInterval = function () {
    console.log(farmInterval);
    if (farmInterval > 100) {
        console.log('Increased by 50');
        farmInterval -= 50;
    } else {
        console.log('maximum reached.');
    }
}
<pre>{{food}}</pre>
<button class="btn btn-default" data-ng-click="startFarming()">Farm</button>
<button class="btn btn-default" data-ng-click="increaseFarmInterval()">Increase Farm Interval</button>
<button class="btn btn-danger" data-ng-click="stopFarming()">Stop</button>
$scope.food=0;
var停止;
var farmInterval=1000;
$scope.startFarming=函数(){
log('farming started…');
如果(角度定义(停止))返回;
停止=$interval(函数(){
$scope.food+=1;
},farmInterval);/100){
console.log(“增加了50”);
时间间隔-=50;
}否则{
console.log('达到最大值');
}
}
我的看法是:

{{food}
农场
增加农场间隔
停止

Plunker版本:

您需要使用$timeout:

注意我是如何在$timeout调用链中转换$timer的。这不仅是更改间隔的良好实践,而且是在浏览器仅实现
setTimeout
时脚本实现
setInterval
的唯一方法

大部分代码已经是您的,但我修改了与
$timer
相关的逻辑,不再使用
$timer
,因此您必须改为注入
$timeout
(或者-这取决于您是否在控制器的其他位置使用
$timer


所以逻辑被改变了:你不杀计时器。相反,您会阻止新的
$timeout
迭代发生,因为如果仔细观察,每个迭代都是在循环的中心逻辑之后显式创建的。

请改用。您需要使用$timeout编写自己的实现。我不知怎的不明白,当您实际减少它时,
会增加50
。。我真的认为它应该起作用,因为它。。。你能设置一个显示问题的plunker吗?我在第一篇文章的底部添加了一个plunker版本。我不知道你为什么提到$timeout$超时只触发一次,我需要每x秒触发一个函数。我不想停止间歇。我只是想加快我的“食物”自动增加的更新速度。你能给我举个例子吗?看看答案。正如他们所说,这与“
$timeout
有关。对于@HarishR:increase——即使他所做的实际上是在减少——指的不是间隔,而是频率——这是间隔的倒数。啊,是的,现在我知道了。真聪明:)太谢谢你了!注意:我不知道这会对性能产生什么影响(老实说,我不知道。也许会慢一点),所以我不知道这是否有利于被滥用。然而,关于“动态频率”的其他替代方案的性能甚至更低。也许,我只是想使用$interval,然后停止并用新的时间间隔再次加载它,因为这个时间间隔大约每小时改变一次。只要我不知道更好的办法,我就保留这个办法。非常感谢:)
$scope.food = 0;
var farmInterval = 1000;
var shouldStop = true;

$scope.startFarming = function () {
    console.log('farming started...');
    shouldStop = false;
    var loop = function () {
        if (!shouldStop) {
            $scope.food += 1;
            $timeout(loop, farmInterval);
            //defers another call to this function in a timeout
            //on each "iteration" like this -actually it's not
            //an interation- you can control what interval time to assign
        }
    };
}

$scope.stopFarming = function () {
    shouldStop = true;
}

$scope.increaseFarmInterval = function () {
    console.log(farmInterval);
    if (farmInterval > 100) {
        console.log('Increased by 50');
        farmInterval -= 50;
    } else {
        console.log('maximum reached.');
    }
}