cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

question about $interval.cancel

ZL_9884170
12-Amethyst

question about $interval.cancel

Hi All,

I want to achieve such an effect through $interval: when I click button A, Call function GunTongstartRotate, the model starts to rotate continuously; when I click button B,Call function GunTongstopRotate, the model stops rotating. The following is the code I wrote.

$scope.GunTongRotate = function() {
		$scope.app.params.QMJrx = $scope.app.params.QMJrx-5;
	};

$scope.GunTongstartRotate = function() {
        //store the interval promise
       $scope.app.params.QMJrx=0;
		 intervalPromise = $interval($scope.GunTongRotate, 50, 0);
      
      
	};

$scope.GunTongstopRotate = function() {
  
  if(angular.isDefined(intervalPromise)){
    $interval.cancel(intervalPromise);
    $scope.app.params.QMJrx=0;
     }
  
	};

The problem is that when I click button A multiple times, the model will turn faster and faster, and clicking button B again will not stop the model from turning. How should I solve this problem?

1 ACCEPTED SOLUTION

Accepted Solutions
sebben
12-Amethyst
(To:ZL_9884170)

Hi,

you can  check if the model is already rotating like this:

var isRotating = 0;
$scope.GunTongRotate = function() {
  $scope.app.params.QMJrx = $scope.app.params.QMJrx-5;
};
$scope.GunTongstartRotate = function() {
  //store the interval promise
  if(isRotating==0){
    $scope.app.params.QMJrx=0;
    intervalPromise = $interval($scope.GunTongRotate, 50, 0);
    isRotating = 1;
  }
};
$scope.GunTongstopRotate = function() {
  if(angular.isDefined(intervalPromise)){
    isRotating = 0;
    $interval.cancel(intervalPromise);
    $scope.app.params.QMJrx=0;
  }
};

 

View solution in original post

4 REPLIES 4
sebben
12-Amethyst
(To:ZL_9884170)

Hi,

you can  check if the model is already rotating like this:

var isRotating = 0;
$scope.GunTongRotate = function() {
  $scope.app.params.QMJrx = $scope.app.params.QMJrx-5;
};
$scope.GunTongstartRotate = function() {
  //store the interval promise
  if(isRotating==0){
    $scope.app.params.QMJrx=0;
    intervalPromise = $interval($scope.GunTongRotate, 50, 0);
    isRotating = 1;
  }
};
$scope.GunTongstopRotate = function() {
  if(angular.isDefined(intervalPromise)){
    isRotating = 0;
    $interval.cancel(intervalPromise);
    $scope.app.params.QMJrx=0;
  }
};

 

ZL_9884170
12-Amethyst
(To:sebben)

Hi sebben,

Thank you for your reply! Your idea is very clever, but unfortunately, the problem still remains that when I click the start rotation button many times, the model still spins faster and faster, and clicking the Stop button doesn't work.

sebben
12-Amethyst
(To:ZL_9884170)

That's odd, it works for me. Did you copy and paste the code or did you edit your code? 

ZL_9884170
12-Amethyst
(To:sebben)

Thanks ! I reviewed the code and found a spelling mistake. When I corrected the spelling mistake, the problem was solved!

Top Tags