2016-07-27 31 views
1

私は今、その目的のために角度jsを学習しています。私は角度関数Jを使用して角度関数Jを使ってサンプルテストアプリケーションを作成しました... ブラウザでアプリケーションを実行すると、いずれかが正しい方向に私を指すしてもらえ角度jsタイマー機能が停止ボタンで機能しない

<!DOCTYPE html> 
<html lang="en-us" ng-app="myApp"> 
<head> 
<title>Angularjs</title> 
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script> 
<script type="text/javascript"> 
var app = angular.module('myApp', []); 
app.controller('NameCtrl', ['$scope','$interval', function ($scope,$interval) { 

    var i = 0; 
    var timer; 
    $scope.message = "Div is Refreshed at " + i + "seconds"; 
     $scope.starttimer = function(){ 

      timer= $interval(function(){ 

         $scope.message = "Div is Refreshed at " + i + "seconds"; 
         i++; 
      },1000) 
      } 
$scope.stoptimer = function(){ 

    if(angular.isUndefined(timer)){ 
     $interval.cancel(timer); 
     timer = 'undefined'; 
     $scope.message = "Timer is killed"; 
    } 
} 
}]) 
</script> 
</head> 

<body> 
    <div ng-controller="NameCtrl"> 
     <input type="button" id="btnStart" ng-click="starttimer()" value="Start" /> 
     <input type="button" id="btnStop" ng-click="stoptimer()" value="Stop"/> 
     {{message}} 
    </div> 
</body> 
カウンタを開始し、私は停止ボタンをクリックしたときに、私は以下 はタイマ機能のための私のコードでタイマーを停止することはできませんよ..

私が上記のコードでタイマーを停止するために間違っている点。

いずれかがこの上で助けてくださいだろう。..

+0

を削除します。それは '(場合ではないshoud! angular.isUndefined(timer)) '(追加された'! 'に注意してください)? –

+0

ありがとうございました。ありがとうございました。私は受け入れる答えとして投稿してください...... –

答えて

1

を条件あなたには、stopTimer関数の文が否定を欠落している場合:

if(angular.isUndefined(timer)){ 

if(!angular.isUndefined(timer)){ 
+0

多くのご支援をいただきありがとうございます。 –

+0

このサイトは正しいですか? –

1

これを試してみてくださいする必要があります、

<!DOCTYPE html> 
 
<html lang="en-us" ng-app="myApp"> 
 
<head> 
 
<title>Angularjs</title> 
 
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script> 
 
<script type="text/javascript"> 
 
var app = angular.module('myApp', []); 
 
app.controller('NameCtrl', ['$scope','$interval', function ($scope,$interval) { 
 

 
    var i = 0; 
 
    var timer; 
 
    $scope.message = "Div is Refreshed at " + i + "seconds"; 
 
     $scope.starttimer = function(){ 
 

 
      timer= $interval(function(){ 
 

 
         $scope.message = "Div is Refreshed at " + i + "seconds"; 
 
         i++; 
 
      },1000) 
 
      } 
 
$scope.stoptimer = function(){ 
 

 
    if(!angular.isUndefined(timer)){ 
 
     console.log('stop'); 
 
     $interval.cancel(timer); 
 
     timer = 'undefined'; 
 
     $scope.message = "Timer is killed"; 
 
     } 
 
    
 
} 
 

 

 
}]) 
 
</script> 
 
</head> 
 

 
<body> 
 
    <div ng-controller="NameCtrl"> 
 
     <input type="button" id="btnStart" ng-click="starttimer()" value="Start" /> 
 
     <input type="button" id="btnStop" ng-click="stoptimer()" value="Stop"/> 
 
     {{message}} 
 
    </div> 
 
</body>

使用if(!angular.isUndefined(timer))の代わりif(angular.isUndefined(timer))

+1

多くのご支援ありがとうございます:) –

1

私は今、それが働いているあなたのコードを編集しました。

<!DOCTYPE html> 
<html lang="en-us" ng-app="myApp"> 
<head> 
<title>Angularjs</title> 
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script> 
<script type="text/javascript"> 
var app = angular.module('myApp', []); 
app.controller('NameCtrl', ['$scope','$interval', function ($scope,$interval) { 
    var i = 0; 
    var timer; 
    $scope.message = "Div is Refreshed at " + i + "seconds"; 
     $scope.starttimer = function(){ 

      timer= $interval(function(){ 

         $scope.message = "Div is Refreshed at " + i + "seconds"; 
         i++; 
      },1000) 
      } 
    $scope.stoptimer = function(){ 
      $interval.cancel(timer); 
      timer = 'undefined'; 
      $scope.message = "Timer is killed"; 
    } 
    $scope.$on('$destroy', function() { 
     $scope.stop(); 
    }); 
}]) 
</script> 
</head> 

<body> 
    <div ng-controller="NameCtrl"> 
     <input type="button" id="btnStart" ng-click="starttimer()" value="Start" /> 
     <input type="button" id="btnStop" ng-click="stoptimer()" value="Stop"/> 
     {{message}} 
    </div> 
</body> 

使用$方法

$scope.$on('$destroy', function() { 
     $scope.stop(); 
    }); 

を破壊する私はは、stopTimer機能であなたのif文に間違いを見つけたと思います。この条件に

angular.isUndefined(timer) 
+0

ご支援いただきありがとうございます:) –

関連する問題