3

I've built a very simply slider with Angular like so:

$scope.slider = {};
$scope.slider.pane = 1;
$scope.slider.auto = true;

var slider = function(){
  $timeout(function(){
    if ($scope.slider.pane === 4) $scope.slider.pane = 1;
    else $scope.slider.pane ++;
    slider();
  }, 4000);
}
slider();

The slider function creates a timeout loop to change the value of slider.pane every 4s. In the HTML I have a link that when clicked sets the value slider.auto to false.

<a href="" ng-click="slider.auto=false">Stop slider</a>

When this is clicked, it needs to stop the timeout loop. It may be in the middle of a cycle at the time, so I need to clear the timeout, but it's inside a function so not sure how to access it.

3
  • 1
    stackoverflow.com/a/21019006/3894168 might help. Commented Apr 15, 2015 at 13:03
  • @Sourabh- thanks but this doesn't work. The promise variable would not be accessible outside my slider function. Commented Apr 15, 2015 at 13:08
  • You can make your promise as a global variable to access it outside. Commented Apr 15, 2015 at 13:11

3 Answers 3

4

Use the $timeout.cancel function:

var timeout;
$scope.cancelTimer = function() {
    $scope.slider.auto=false;
    $timeout.cancel(timeout);
};

var slider = function(){
  timeout = $timeout(function(){
    if ($scope.slider.pane === 4) $scope.slider.pane = 1;
    else $scope.slider.pane ++;
    slider();
  }, 4000);
}
slider();

//HTML
<a href="" ng-click="cancelTimer()">Stop slider</a>
Sign up to request clarification or add additional context in comments.

5 Comments

Isn't the timeout variable scoped within the slider function and therefore not accessible within the cancelTimer function?
Ah no I see you've made it global first
yeah its visible in both functions
Yeh that works great thanks. Don't know why I didn't think to create it as a global variable like that
it is not global variable, since $scope is inside a function.
0

Try:

$scope.slider = {};
$scope.slider.pane = 1;
$scope.slider.auto = true;
var promise;
var slider = function(){
  promise = $timeout(function(){
    if ($scope.slider.pane === 4) $scope.slider.pane = 1;
    else $scope.slider.pane ++;
    slider();
  }, 4000);
}
$scope.autoSliderFalse = function() {
    $scope.slider.auto = false;
    if(promise)
       $timeout.cancel(promise);
});
slider();

HTML

<a href="" ng-click="autoSliderFalse()">Stop slider</a>

Comments

0

You can use the cancel method as some people suggested here.

I actually think in your case you should use $interval instead of $timeout.

var interval = $interval(function(){
  if ($scope.slider.pane === 4) {
    $scope.slider.pane = 1;
  }
  else {
    $scope.slider.pane ++;
  }    
}, 4000);

$scope.stopSlider = function(){
  $interval.cancel(interval);
};

//html
<a href="" ng-click="stopSlider()">Stop slider</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.