Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an $interval that calls a certain number of times.

When the interval has finished running, I want it to call a final reset function. How can I do this?

ie.

  $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/

like image 651
dwjohnston Avatar asked Sep 26 '22 12:09

dwjohnston


1 Answers

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

So:

$interval(lerp, 500, 5).then(fin);
like image 102
user3707125 Avatar answered Oct 11 '22 14:10

user3707125