获取NaN而不是数字/值

时间:2016-04-17 20:13:00

标签: javascript html angularjs ionic-framework

应该是简单的语法错误,但我不知道它是什么

控制器:

    $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>

结果是“3”..“2”......“NaN”....任何帮助都将不胜感激

1 个答案:

答案 0 :(得分:2)

您未在$scope.startTimeouter中向timeout传递任何内容,因此number的值未定义。而是将您的功能设置为:

$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);  
    } 

}