Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2014 09:15
Show Gist options
  • Save anonymous/8266141 to your computer and use it in GitHub Desktop.
Save anonymous/8266141 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 5, 2014.
    17 changes: 17 additions & 0 deletions jsbin.oyOQENe.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <!DOCTYPE html>
    <html ng-app="docsTimeDirective">
    <head>
    <meta name="description" content="Simple Angular JS countdown timer" />
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
    <div ng-controller="Ctrl2">
    <label for="minutes">Minutes<input id="minutes" type="text" ng-model="minutes"></label>
    <label for="seconds">Seconds<input id="seconds" type="text" ng-model="seconds"></label>
    <button ng-click="startTimer()">Start</button>
    <h3>{{timeLeft}}</h3>
    </div>
    </body>
    </html>
    40 changes: 40 additions & 0 deletions jsbin.oyOQENe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    angular.module('docsTimeDirective', [])
    .controller('Ctrl2', function($scope, $interval, dateFilter) {

    $scope.format = 'mm:ss';
    $scope.minutes = 0;
    $scope.seconds = 5;
    $scope.total = $scope.minutes * 60 + $scope.seconds;
    $scope.timeLeft = dateFilter( $scope.total * 1000, $scope.format );

    $scope.startTimer = function(){

    var updateTimer = function(){

    $scope.timeLeft = dateFilter( $scope.total * 1000, $scope.format );

    };

    var countDown = $interval(function(){

    console.log($scope.timeLeft);

    if ( $scope.total !== 0 && $scope.total !== '0' ) {

    $scope.total--;
    updateTimer();

    } else {

    $scope.total = $scope.minutes * 60 + $scope.seconds;
    updateTimer();

    $interval.cancel(countDown);

    }

    }, 1000);

    };

    });