javascript - How to make a seconds countdown using angular? - Stack Overflow

I'm looking on how to make a 60 seconds countdown using angular js.I want to show the countdown on

I'm looking on how to make a 60 seconds countdown using angular js.

I want to show the countdown on the page ! and when the countdown is finished, the controller should reload to execute the code again ! and get the update json object !

my controller looks like :

.controller('todaymatches', function($rootScope,$scope, $http) {

    $http.get("/")
        .success(function (response) {
            $scope.matches = response;
        });
})

I'm made a code ! I'm not sure if this works properly ! anyway it's not working on my app.

$scope.countdown = function() {
    stopped = $timeout(function() {
       console.log($scope.counter);
     $scope.counter--;   
     $scope.countdown();   
    }, 1000);
  };

I'm looking on how to make a 60 seconds countdown using angular js.

I want to show the countdown on the page ! and when the countdown is finished, the controller should reload to execute the code again ! and get the update json object !

my controller looks like :

.controller('todaymatches', function($rootScope,$scope, $http) {

    $http.get("http://www.domaine./updatedjson/")
        .success(function (response) {
            $scope.matches = response;
        });
})

I'm made a code ! I'm not sure if this works properly ! anyway it's not working on my app.

$scope.countdown = function() {
    stopped = $timeout(function() {
       console.log($scope.counter);
     $scope.counter--;   
     $scope.countdown();   
    }, 1000);
  };
Share Improve this question asked Jun 1, 2015 at 15:00 Yasser B.Yasser B. 8357 gold badges21 silver badges34 bronze badges 1
  • Take a look at Angular's directives documentation here : docs.angularjs/guide/directive Look for "Creating a Directive that Manipulates the DOM". – cl3m Commented Jun 1, 2015 at 15:03
Add a ment  | 

4 Answers 4

Reset to default 3

Here is a simple countdown example:

HTML

<!doctype html>
<html ng-app>
<head>
    <script src="http://code.angularjs/angular-1.0.0rc11.min.js"></script>
    <script src="http://documentcloud.github./underscore/underscore-min.js"></script>
</head>
<body>
   <div ng-controller="CountdownController">
      {{counter}}
   </div>
</body>
</html>

Javascript

function CountdownController($scope,$timeout) {
    $scope.counter = 60;
    $scope.onTimeout = function(){
        if ($scope.counter > 0) {
            $scope.counter--;
            mytimeout = $timeout($scope.onTimeout,1000);
        } else {
            $scope.counter = 60;
        }
    }
    var mytimeout = $timeout($scope.onTimeout,1000);   
}

Demo

'use strict';

var ngApp = angular.module('myApp', ['Test']);

var c1 = angular.module('Test', []);
c1.controller('Ctrl1', function ($scope, $timeout) {

    $scope.coutDown = function () {

        $scope.onTimeout = function () {
            console.log("value", $scope.value);
            $scope.value = $scope.value - 1;
            return $scope.coutDown($scope.value);
        };
        var delay = $timeout($scope.onTimeout, 1000);

        if ($scope.value < 1) {
            $timeout.cancel(delay);
            return true;
        }
        return false;
    };

    $scope.value = 5;
    $scope.coutDown();

});


<div ng-app="myApp">
    <div ng-controller="Ctrl1">
        <h1>{{value}}</h1>
    </div>
</div>

http://jsfiddle/pbxaD/49/

if you want to use $timeout you have to inject it. But why don't you just call the update method in a certain interval?

.controller('todaymatches', function($rootScope,$scope, $http, $interval) {

    var update = function() {
        $http.get("http://www.domaine./updatedjson/")
        .success(function (response) {
            $scope.matches = response;
        });
    };

    var initialize = function() {
        $interval(function() {
            update();
        }, 60 * 1000)
    };

    initialize();
})

I tried this for the count down and it seems to work.

app.controller('CountDownController', function($scope, $timeout) {
    $scope.counter = 60;
    $scope.countdown = function() {
        if ($scope.counter === 0) {
            // do your reload and execute here
            //Just reset the counter if you just want it to count again
            $scope.counter = 60;
            return;
        } else {
            $timeout(function() {
                console.log($scope.counter);
                $scope.counter--;
                $scope.countdown();
            }, 1000);
        }
    };
    $scope.countdown();
});

You could tie up the various things you want to do inside the if condition of the above code as mented. I just reset the counter after counting down to 0.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341426a4623344.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信