Skip to content

Commit

Permalink
refactor(carousel): remove duplicate code
Browse files Browse the repository at this point in the history
Move go function out of restartTimer as it doesn't need to be re-created
during each interval.
  • Loading branch information
chrisirhc authored and pkozlowski-opensource committed Dec 23, 2013
1 parent 5b9d929 commit 0b39994
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/carousel/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,32 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
};

$scope.$watch('interval', restartTimer);
$scope.$on('$destroy', resetTimer);

function restartTimer() {
resetTimer();
var interval = +$scope.interval;
if (!isNaN(interval) && interval>=0) {
currentTimeout = $timeout(timerFn, interval);
}
}

function resetTimer() {
if (currentTimeout) {
$timeout.cancel(currentTimeout);
currentTimeout = null;
}
function go() {
if (isPlaying) {
$scope.next();
restartTimer();
} else {
$scope.pause();
}
}
var interval = +$scope.interval;
if (!isNaN(interval) && interval>=0) {
currentTimeout = $timeout(go, interval);
}

function timerFn() {
if (isPlaying) {
$scope.next();
restartTimer();
} else {
$scope.pause();
}
}

$scope.play = function() {
if (!isPlaying) {
isPlaying = true;
Expand All @@ -130,10 +138,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
$scope.pause = function() {
if (!$scope.noPause) {
isPlaying = false;
if (currentTimeout) {
$timeout.cancel(currentTimeout);
currentTimeout = null;
}
resetTimer();
}
};

Expand Down Expand Up @@ -166,12 +171,6 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
}
};

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

/**
Expand Down

0 comments on commit 0b39994

Please sign in to comment.