AngularJS Live Example

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div ng-controller = "fessCntrl"> 
    <pre>{{counter}}</pre>
</div>

JavaScript

var fessmodule = angular.module('myModule', []);

fessmodule.controller('fessCntrl', function ($scope, $timeout) {
  
    $scope.counter = 0;
    
    var timer;

       function myLoop(){
           
            $scope.counter++;
           
       // When the timeout is defined, it returns a
                    // promise object.
                    timer = $timeout(
                        function() { 
                            console.log( "Timeout executed", Date.now() ); 
                        },
                        1000
                    );
  
                    
                    timer.then(
                        function() { 
                            console.log( "Timer resolved!");
							myLoop();
 
                        },
                        function() { 
                            console.log( "Timer rejected!" ); 
                        }
                    );
                }

                myLoop();
 
                    // When the DOM element is removed from the page,
                    // AngularJS will trigger the $destroy event on
                    // the scope. 
					// Cancel timeout
                    $scope.$on(
                        "$destroy",
                        function( event ) { 
                            $timeout.cancel( timer ); 
                        }
                    );
    
});

fessmodule.$inject = ['$scope','$timeout'];