MehdiN
MehdiN

Reputation: 351

Getting NaN instead of number/value

Should be simple syntax error but I don't know what it is

Controller:

    $scope.startCounter=3;

    $scope.startTimeouter = function (number) {  
      $scope.startCounter = number - 1;  
      mytimeouter = $timeout($scope.startTimeouter, 1000);  

      if($scope.startCounter<=0){
        $timeout.cancel(mytimeouter); 
        $scope.startTimeout();
      } 
        $scope.startTimerBtner=function(){
      $scope.startTimeouter();
    }


    $scope.stopTimerBtner = function () {  
        $timeout.cancel(mytimeouter);  
    } 
  }

HTML:

<a ng-click="startTimeouter(3);" >Click to Start Timer</a>

And the result is "3".."2"..."NaN"....any help would be greatly appreciated

Upvotes: 0

Views: 674

Answers (1)

A.J. Uppal
A.J. Uppal

Reputation: 19264

You are not passing anything into $scope.startTimeouter in your timeout, so the value for number is undefined. Instead, set your function to:

$scope.startCounter=3;

$scope.startTimeouter = function (number) {
      $scope.startCounter = number - 1;  
      mytimeouter = $timeout(function(){$scope.startTimeouter($scope.startCounter)}, 1000);  

      if($scope.startCounter<=0){
        $timeout.cancel(mytimeouter); 
        $scope.startTimeout();
      } 
        $scope.startTimerBtner=function(){
      $scope.startTimeouter();
    }


    $scope.stopTimerBtner = function () {  
        $timeout.cancel(mytimeouter);  
    } 

}

Upvotes: 2

Related Questions