Confusion about how the promise returned by $interval works compared to $timeout in Angular

后端 未结 2 824
无人及你
无人及你 2021-01-21 00:52

I\'m having an issue understanding how the promise returned by $interval works in Angular.

Let\'s say in the following example, we have a simple \"api\" factory with a

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 01:20

    Added the count value to the example that the DIMM Reaper's provided in a comment to Christophe L's answer:

    var app = angular.module("app", []);
    
    app.run(function ($interval) {
        console.log('This fiddle will self destruct...');
        
        var timer = $interval(function (count) {
            console.log('tick... ' + count);
        }, 500, 4, false);
        
        timer.then(function(res) {
            console.log("BOOM! " + res);
        });
    });
    
    
    
    

    Output:

    This fiddle will self destruct...
    tick... 1
    tick... 2
    tick... 3
    tick... 4
    BOOM! 4
    

    The final result is the final count.

提交回复
热议问题