Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(carousel): cancel timer on scope destruction
Browse files Browse the repository at this point in the history
Closes #1414
  • Loading branch information
chrisirhc authored and pkozlowski-opensource committed Dec 23, 2013
1 parent c9acebb commit 5b9d929
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/carousel/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
function restartTimer() {
if (currentTimeout) {
$timeout.cancel(currentTimeout);
currentTimeout = null;
}
function go() {
if (isPlaying) {
Expand All @@ -131,6 +132,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
isPlaying = false;
if (currentTimeout) {
$timeout.cancel(currentTimeout);
currentTimeout = null;
}
}
};
Expand Down Expand Up @@ -163,6 +165,13 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
currentIndex--;
}
};

$scope.$on('$destroy', function () {
if (currentTimeout) {
$timeout.cancel(currentTimeout);
currentTimeout = null;
}
});
}])

/**
Expand Down
14 changes: 14 additions & 0 deletions src/carousel/test/carousel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ describe('carousel', function() {
expect(ctrl.slides.length).toBe(1);
expect(ctrl.currentSlide).toBe(ctrl.slides[0]);
});

it('issue 1414 - should not continue running timers after scope is destroyed', function() {
spyOn(scope, 'next').andCallThrough();
scope.interval = 2000;
scope.$digest();

$timeout.flush();
expect(scope.next.calls.length).toBe(1);

scope.$destroy();

$timeout.flush(scope.interval);
expect(scope.next.calls.length).toBe(1);
});
});
});
});

0 comments on commit 5b9d929

Please sign in to comment.