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

Commit 5b9d929

Browse files
chrisirhcpkozlowski-opensource
authored andcommitted
fix(carousel): cancel timer on scope destruction
Closes #1414
1 parent c9acebb commit 5b9d929

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/carousel/carousel.js

+9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
106106
function restartTimer() {
107107
if (currentTimeout) {
108108
$timeout.cancel(currentTimeout);
109+
currentTimeout = null;
109110
}
110111
function go() {
111112
if (isPlaying) {
@@ -131,6 +132,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
131132
isPlaying = false;
132133
if (currentTimeout) {
133134
$timeout.cancel(currentTimeout);
135+
currentTimeout = null;
134136
}
135137
}
136138
};
@@ -163,6 +165,13 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
163165
currentIndex--;
164166
}
165167
};
168+
169+
$scope.$on('$destroy', function () {
170+
if (currentTimeout) {
171+
$timeout.cancel(currentTimeout);
172+
currentTimeout = null;
173+
}
174+
});
166175
}])
167176

168177
/**

src/carousel/test/carousel.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ describe('carousel', function() {
294294
expect(ctrl.slides.length).toBe(1);
295295
expect(ctrl.currentSlide).toBe(ctrl.slides[0]);
296296
});
297+
298+
it('issue 1414 - should not continue running timers after scope is destroyed', function() {
299+
spyOn(scope, 'next').andCallThrough();
300+
scope.interval = 2000;
301+
scope.$digest();
302+
303+
$timeout.flush();
304+
expect(scope.next.calls.length).toBe(1);
305+
306+
scope.$destroy();
307+
308+
$timeout.flush(scope.interval);
309+
expect(scope.next.calls.length).toBe(1);
310+
});
297311
});
298312
});
299313
});

0 commit comments

Comments
 (0)