Javascript SetTimeout在angular js中失败?

Javascript SetTimeout在angular js中失败?,javascript,angularjs,settimeout,Javascript,Angularjs,Settimeout,我要增加每秒钟的计数。所以我使用do while循环,但它会使浏览器崩溃 代码: do{ $scope.timer=0; log($scope.timer); setTimeout(函数(){ $scope.$apply(函数(){$scope.timer=$scope.timer+1; 返回$scope.timer;}); }, 1000); }而($scope.timer

我要增加每秒钟的计数。所以我使用do while循环,但它会使浏览器崩溃

代码:

do{
$scope.timer=0;
log($scope.timer);
setTimeout(函数(){
$scope.$apply(函数(){$scope.timer=$scope.timer+1;
返回$scope.timer;});
}, 1000);
}而($scope.timer<$scope.level.\u秒\u每个问题);

你能告诉我哪里出了问题吗?

请在下面找到代码

function Ctrl($scope, $timeout) 
{
  $scope.timeInMs = 0;  
  var countUp = function() {
    $scope.timeInMs+= 1;
    $timeout(countUp, 1000);
  }    
  $timeout(countUp, 1000);
}


请在下面找到代码

function Ctrl($scope, $timeout) 
{
  $scope.timeInMs = 0;  
  var countUp = function() {
    $scope.timeInMs+= 1;
    $timeout(countUp, 1000);
  }    
  $timeout(countUp, 1000);
}


首先,有一个本地Angular服务,它允许您在不调用
$scope.$apply
的情况下执行超时

所以,你可以这样做:

function MyController($scope, $timeout) {
    $scope.timer = 0;
    $scope.level = {
      // I assume this object is declared in a parent $scope in your code.
      // I define it here just so the sample will work.
      _seconds_per_question: 10
    };

    $timeout(increment, 1000);

    function increment() {
        if ($scope.timer < $scope.level._seconds_per_question) {
          console.log($scope.timer);
            $scope.timer ++;
            $timeout(increment, 1000);
        }
    }
}
函数MyController($scope,$timeout){
$scope.timer=0;
$scope.level={
//我假设这个对象是在代码中的父$scope中声明的。
//我在这里定义它,只是为了让示例能够工作。
_每题秒数:10
};
$timeout(增量,1000);
函数增量(){
如果($scope.timer<$scope.level.\u秒\u每个问题){
log($scope.timer);
$scope.timer++;
$timeout(增量,1000);
}
}
}

演示:

首先,有一个本机Angular服务,它允许您在不调用
$scope.$apply的情况下执行超时

所以,你可以这样做:

function MyController($scope, $timeout) {
    $scope.timer = 0;
    $scope.level = {
      // I assume this object is declared in a parent $scope in your code.
      // I define it here just so the sample will work.
      _seconds_per_question: 10
    };

    $timeout(increment, 1000);

    function increment() {
        if ($scope.timer < $scope.level._seconds_per_question) {
          console.log($scope.timer);
            $scope.timer ++;
            $timeout(increment, 1000);
        }
    }
}
函数MyController($scope,$timeout){
$scope.timer=0;
$scope.level={
//我假设这个对象是在代码中的父$scope中声明的。
//我在这里定义它,只是为了让示例能够工作。
_每题秒数:10
};
$timeout(增量,1000);
函数增量(){
如果($scope.timer<$scope.level.\u秒\u每个问题){
log($scope.timer);
$scope.timer++;
$timeout(增量,1000);
}
}
}

演示:

你能给出完整的控制器代码吗…@parthicool05,我在interval函数的内部使用此代码。只有这段代码。我自己失败了。朋友,你在哪里写的这段代码,它在控制器里面还是不在控制器里面。你能给出完整的控制器代码吗…@parthicool05,我在间隔函数里面使用这段代码。只有这段代码。我自己失败了。朋友,你在哪里写的这段代码,它是否在控制器内部。@CaspNZ:我修改了代码,也为它创建了JSFIDLEthis@Archana,你能告诉我jsfiddle链接在哪里吗?@Anusha:你看不到我上面共享的jsfiddle链接和我的答案吗?[示例]您能告诉我您是否可以访问该页面吗?@Archana,对不起,页面未刷新before@CaspNZ:我已经修改了代码,并为其创建了JSFIDLEthis@Archana,你能告诉我jsfiddle链接在哪里吗?@Anusha:你看不到我上面共享的jsfiddle链接和我的答案吗?[示例]您能告诉我您是否可以访问该页面吗?@Archana,对不起,之前页面没有刷新