-1

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 ?

4
  • 3
    Don't use jQuery in Angular.js. Commented Sep 6, 2014 at 17:35
  • Create a jsbin. Also, call the setTimeout after you have decremented the countDown var. Commented Sep 6, 2014 at 17:40
  • 1
    In angularjs $timeout should be favoured over setTimeout Commented Sep 6, 2014 at 17:45
  • 1
    Also, having countDown and countDown_text seems unnecessary since they are identical in everyway. I would stick with $scope.countDown and use that in your view. Commented Sep 6, 2014 at 18:07

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.