3

I have this code for image slider and next prev and auto change the image function

scope.next = function () {
    scope.currentIndex < scope.images.length - 1 ? scope.currentIndex++ : scope.currentIndex = 0;
};

scope.prev = function () {
    scope.currentIndex > 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);
});

and in slider template I have the arrows link for next and prev function

  <div class="arrows">
    <a href="#" ng-click="prev()">
      <img src="tasavir/omgh/left-arrow.png" />
    </a>
    <a href="#" ng-click="next()">
      <img src="tasavir/omgh/right-arrow.png" />
    </a>
  </div>

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.

this is the full doc about image slider

I create the JSFiddle for this please look at this

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Syd Amir
  • 465
  • 7
  • 19

3 Answers3

3

This is my third try: https://jsfiddle.net/koljada/8cLw6wch/22/

var timer = $interval(function () {
    scope.changeImage();
}, 5000);

scope.next = function () {
    $interval.cancel(timer);

    timer = $interval(function () {
        scope.changeImage();
    }, 5000);
};

scope.changeImage = function () {
    scope.currentIndex < scope.images.length - 1
        ? scope.currentIndex++
        : (scope.currentIndex = 0);
};
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59
  • tnx, but you cancel the timer and did not change image automatic when using the next btn – Syd Amir Aug 13 '15 at 14:47
  • I don't understand you. When i clicked on the next btn, an image changes once and interval canceled. – Roman Koliada Aug 13 '15 at 15:02
  • see, we want the interval cleared when we click on the next btn, when we several time click on the next btn the interval did not cleared in the 5s second we clicked the next and image changed in that second interval changed the image too, so 2 imaged was slides – Syd Amir Aug 13 '15 at 15:48
  • in fact we want every time we clicked on the next btn the interval was stop and restart again and changed image in 5 second later – Syd Amir Aug 13 '15 at 15:50
2

You could do by setting timeout from $scope.next function by checking a flag.

Markup

<div class="arrows">
    <a href="#" ng-click="prev()">
      <img src="tasavir/omgh/left-arrow.png" />
    </a>
    <a href="#" ng-click="next(true)">
      <img src="tasavir/omgh/right-arrow.png" />
    </a>
</div>

Code

var timer;
var sliderFunc = function () {
    timer = $timeout(function () {
        scope.next(false);
    }, 5000);
};

scope.next = function(setTimeoutToNext){
    scope.currentIndex < scope.images.length - 1 ? scope.currentIndex++ : scope.currentIndex = 0;
    if(setTimeoutToNext)
      $timeout.cancel(timer); //here it will clear the timeout
}

Working Fiddle

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Hi, unfortunately did note resolve the problem, I created JSFiddle for this please take a look at that and fixed in that tanx https://jsfiddle.net/hemedani/8cLw6wch/14/ – Syd Amir Aug 13 '15 at 14:12
  • @SydAmir look at the edit..Now it fixed I wrote different line instead of `$timeout.cancel(timer)` – Pankaj Parkar Aug 13 '15 at 17:38
2

Hi @Roman and @Pankaj tanks for huge helping... Fix with this code :

     scope.next = function () {                 
            $interval.cancel(timer);
            scope.changeImage(); // just add this line
            timer = $interval(function () {
             scope.changeImage();
            }, 5000);                 
     };

in this version of @Roman edited.

The Final version

Tanx guy ...

Syd Amir
  • 465
  • 7
  • 19