Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Automatic counter with $timeout

  • $timeout
  • setTimeout
<script src="angular.min.js"></script>
<script>
angular.module('CounterApp', [])
    .controller('CounterController', ['$scope', '$timeout', function($scope, $timeout) {
        $scope.counter = 0;
        var updateCounter = function() {
            $scope.counter++;
            $timeout(updateCounter, 1000);
        };
        updateCounter();
    }]);
</script>
<div ng-app="CounterApp">
   <div ng-controller="CounterController">
   {{counter}}
   </div>
</div>

$timeout