[英]How can I call a function after an interval has finished?
I have an $interval
that calls a certain number of times. 我有一个
$interval
,调用了一定次数。
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/ 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
该承诺将在间隔的每个滴答时得到通知,并将在计数迭代后得到解决
So: 所以:
$interval(lerp, 500, 5).then(fin);
$interval
returns a promise, which will be resolved when it has finished executing all of its iterations. $interval
返回一个promise,它将在完成所有迭代的执行后解析。
Just use promise.then
to execute the final task.. 只需使用
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);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.