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

Commit 0b39994

Browse files
chrisirhcpkozlowski-opensource
authored andcommitted
refactor(carousel): remove duplicate code
Move go function out of restartTimer as it doesn't need to be re-created during each interval.
1 parent 5b9d929 commit 0b39994

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/carousel/carousel.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,32 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
103103
};
104104

105105
$scope.$watch('interval', restartTimer);
106+
$scope.$on('$destroy', resetTimer);
107+
106108
function restartTimer() {
109+
resetTimer();
110+
var interval = +$scope.interval;
111+
if (!isNaN(interval) && interval>=0) {
112+
currentTimeout = $timeout(timerFn, interval);
113+
}
114+
}
115+
116+
function resetTimer() {
107117
if (currentTimeout) {
108118
$timeout.cancel(currentTimeout);
109119
currentTimeout = null;
110120
}
111-
function go() {
112-
if (isPlaying) {
113-
$scope.next();
114-
restartTimer();
115-
} else {
116-
$scope.pause();
117-
}
118-
}
119-
var interval = +$scope.interval;
120-
if (!isNaN(interval) && interval>=0) {
121-
currentTimeout = $timeout(go, interval);
121+
}
122+
123+
function timerFn() {
124+
if (isPlaying) {
125+
$scope.next();
126+
restartTimer();
127+
} else {
128+
$scope.pause();
122129
}
123130
}
131+
124132
$scope.play = function() {
125133
if (!isPlaying) {
126134
isPlaying = true;
@@ -130,10 +138,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
130138
$scope.pause = function() {
131139
if (!$scope.noPause) {
132140
isPlaying = false;
133-
if (currentTimeout) {
134-
$timeout.cancel(currentTimeout);
135-
currentTimeout = null;
136-
}
141+
resetTimer();
137142
}
138143
};
139144

@@ -166,12 +171,6 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
166171
}
167172
};
168173

169-
$scope.$on('$destroy', function () {
170-
if (currentTimeout) {
171-
$timeout.cancel(currentTimeout);
172-
currentTimeout = null;
173-
}
174-
});
175174
}])
176175

177176
/**

0 commit comments

Comments
 (0)