间隔结束后如何调用函数?

How can I call a function after an interval has finished?

我有一个 $interval 调用了一定次数。

当间隔结束时 运行,我希望它调用最终重置函数。我怎样才能做到这一点?

即。

  $scope.clickme = function() {
    var i = 0;

    function lerp() {
      alert(i++);
    }

    function fin(){    //how do I call this function? 
        alert ("all done!")
    }

    $interval(lerp, 500, 5);
  };

JSFiddle:http://jsfiddle.net/h4cn32e6/1/

The return value of registering an interval function is a promise. This promise will be notified upon each tick of the interval, and will be resolved after count iterations

所以:

$interval(lerp, 500, 5).then(fin);

$interval returns 一个承诺,当它完成所有迭代时将被解决。

只需使用promise.then执行最终任务..

  $scope.clickme = function() {
    var i = 0;

    function lerp() {
      alert(i++);
    }

    function fin(){
        alert ("all done!")
    }

    var promise = $interval(lerp, 500, 5);

    promise.then(fin);
  };

http://jsfiddle.net/0wuwnhxo/