Search code examples
javascriptangularjseventscountdown

AngularJS - simple Count-Down using requestAnimationFrame (start/stop/restart)


This is the little plnkr

I am just trying printing count down seconds from 11 to 0 and then restart it when finished.

The problem is it won't print seconds in the html also tried with $apply but i get digest error. Also when restarting the countdown i got problems, seconds are not updated correctly...

How can i achieve this more easily?

Any help appreciated thanks


Solution

  • Here you go

    (function withAngular(angular) {
    
    'use strict';
    
    angular.module('app', ['ngRoute'])
    .controller('Ctrl', function($scope, $window, $timeout) {
    
      $scope.countSeconds = 11;
      $scope.updateCountdown = function updateCountdown(ms) {
    
        $timeout(function() {
    
          if($scope.countSeconds === 0){
          $scope.countSeconds = 11;
          }
         else{$scope.countSeconds--;   }
         $scope.updateCountdown($scope.countSeconds);   
        }, 1000);
      };
    
    
    
      $scope.updateCountdown(0);
    
    
    
    });
      }(angular));