Search code examples
javascriptangularjscountdown

js countdown break my angularjs code


I'm little disapointed about my countdown function :

/* countDowner */
var countDown = 5;
function countDowner() {
    if (countDown < 0) {
        $("#warning").fadeOut(2000);
        var countDown = 0;
        return; // quit
    } else {
        $scope.countDown_text = countDown; // update scope
        setTimeout(countDowner, 1000); // loop it again
        countDown--; // -1
    }
}
$scope.countDown_text = countDown;
countDowner();

i put it in a angularjs ctrl and it's break my code :/ there is an error but it's for other code. when i remove my countdown all is working good. what's wrong in my countdown ?


Solution

  • The reason the code does not work is because you have re-declared the countDown variable.

    // initialization
    var countDown = 5;
    // later ...
    var countDown = 0;
    

    Here is a working example http://jsfiddle.net/6vkyezbu/