简体   繁体   English

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

[英]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);
  };

http://jsfiddle.net/0wuwnhxo/ http://jsfiddle.net/0wuwnhxo/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在浏览器事件上调用触发器后,如何在函数完成后调用它? - After calling trigger on a browser event, how can I call a function when it has finished? Jquery:如何在第一个参数完成后用不同的参数回调相同的函数? - Jquery: How can I call back the same function with a different parameter after the 1st one has finished? grecaptcha.execute() 完成执行后如何调用函数 - 由事件触发? - How can I call a function after grecaptcha.execute() has finished executing - triggered by an event? fetch() 完成后如何调用 function - How to call function after fetch() has finished AngularJS:如何在一定时间间隔后调用函数? - AngularJS: how can I call a function after certain time interval? React.js-如何在另一个函数完成后调用setState函数 - React.js - how do I call a setState function after another one has finished 如何仅在页面加载完成后以及调用另一个函数之后才调用函数 - How to call a function only after page has finished loading and after another function has been called 页面加载完成后如何调用函数 - How can I make a function get called after a page has finished loading 元素渲染完成后如何立即运行函数? - How can I run a function as soon as an element has finished rendering? 在function1完成后调用function2 - Call a function2 after function1 has already finished
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM