title: How can clear $timeout in angularjs
body: <p>I have this code for image slider and next prev and auto change the image function</p>
  
  <pre><code>scope.next = function () {
      scope.currentIndex &lt; scope.images.length - 1 ? scope.currentIndex++ : scope.currentIndex = 0;
  };
  
  scope.prev = function () {
      scope.currentIndex &gt; 0 ? scope.currentIndex-- : scope.currentIndex = scope.images.length - 1;
  };
  
  scope.$watch('currentIndex', function () {
      scope.images.forEach(function (image) {
      image.visible = false;
      });
  
      scope.images[scope.currentIndex].visible = true;
  });
  
  var timer;
  var sliderFunc = function () {
      timer = $timeout(function () {
          scope.next();
          timer = $timeout(sliderFunc, 5000);
      }, 5000);
  };
  
  sliderFunc();
  
  scope.$on('$destroy', function () {
      $timeout.cancel(timer);
  });
  </code></pre>
  
  <p>and in slider template I have the arrows link for next and prev function</p>
  
  <pre><code>  &lt;div class="arrows"&gt;
      &lt;a href="#" ng-click="prev()"&gt;
        &lt;img src="tasavir/omgh/left-arrow.png" /&gt;
      &lt;/a&gt;
      &lt;a href="#" ng-click="next()"&gt;
        &lt;img src="tasavir/omgh/right-arrow.png" /&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>I just want to add clear $timeout function when user click on the next or prev btn and each time the user click on the next or prev btn the timer was clear and change image in 5s later.</p>
  
  <p>this is the full doc about <a href="http://www.sitepoint.com/creating-slide-show-plugin-angularjs/" rel="nofollow">image slider</a></p>
  
  <p>I create the JSFiddle for this <a href="https://jsfiddle.net/hemedani/8cLw6wch/15/" rel="nofollow">please look at this</a></p>