This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
fix(carousel): using setInterval to get rid of dangling $timeouts #1423
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,11 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
var self = this, | ||
slides = self.slides = [], | ||
currentIndex = -1, | ||
currentTimeout, isPlaying; | ||
currentInterval, isPlaying; | ||
self.currentSlide = null; | ||
|
||
/* direction: "prev" or "next" */ | ||
self.select = function(nextSlide, direction) { | ||
self.select = function(nextSlide, direction, clicked) { | ||
var nextIndex = slides.indexOf(nextSlide); | ||
//Decide direction if it's not given | ||
if (direction === undefined) { | ||
|
@@ -57,8 +57,10 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
} | ||
self.currentSlide = nextSlide; | ||
currentIndex = nextIndex; | ||
//every time you change slides, reset the timer | ||
restartTimer(); | ||
//only reset the timer when this method is called from a click | ||
if(clicked === true){ | ||
restartTimer(); | ||
} | ||
} | ||
function transitionDone(next, current) { | ||
angular.extend(next, {direction: '', active: true, leaving: false, entering: false}); | ||
|
@@ -72,21 +74,21 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
return slides.indexOf(slide); | ||
}; | ||
|
||
$scope.next = function() { | ||
$scope.next = function(clicked) { | ||
var newIndex = (currentIndex + 1) % slides.length; | ||
|
||
//Prevent this user-triggered transition from occurring if there is already one in progress | ||
if (!$scope.$currentTransition) { | ||
return self.select(slides[newIndex], 'next'); | ||
return self.select(slides[newIndex], 'next', clicked); | ||
} | ||
}; | ||
|
||
$scope.prev = function() { | ||
$scope.prev = function(clicked) { | ||
var newIndex = currentIndex - 1 < 0 ? slides.length - 1 : currentIndex - 1; | ||
|
||
//Prevent this user-triggered transition from occurring if there is already one in progress | ||
if (!$scope.$currentTransition) { | ||
return self.select(slides[newIndex], 'prev'); | ||
return self.select(slides[newIndex], 'prev', clicked); | ||
} | ||
}; | ||
|
||
|
@@ -103,34 +105,37 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
}; | ||
|
||
$scope.$watch('interval', restartTimer); | ||
|
||
function restartTimer() { | ||
if (currentTimeout) { | ||
$timeout.cancel(currentTimeout); | ||
if (currentInterval) { | ||
clearInterval(currentInterval); | ||
} | ||
function go() { | ||
if (isPlaying) { | ||
$scope.next(); | ||
restartTimer(); | ||
} else { | ||
$scope.pause(); | ||
} | ||
$timeout(angular.noop); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. causes digest errors. This is the safe way to run a digest without checking for the $$phase on the scope. |
||
} | ||
var interval = +$scope.interval; | ||
if (!isNaN(interval) && interval>=0) { | ||
currentTimeout = $timeout(go, interval); | ||
currentInterval = setInterval(go, interval); | ||
} | ||
} | ||
|
||
$scope.play = function() { | ||
if (!isPlaying) { | ||
isPlaying = true; | ||
restartTimer(); | ||
} | ||
}; | ||
|
||
$scope.pause = function() { | ||
if (!$scope.noPause) { | ||
isPlaying = false; | ||
if (currentTimeout) { | ||
$timeout.cancel(currentTimeout); | ||
if (currentInterval) { | ||
clearInterval(currentInterval); | ||
} | ||
} | ||
}; | ||
|
@@ -163,6 +168,12 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
currentIndex--; | ||
} | ||
}; | ||
|
||
$scope.$on('$destroy', function(){ | ||
clearInterval(currentInterval); | ||
}); | ||
|
||
|
||
}]) | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshkurz Removing this one I think has a side effect on the time between transitions of the slides. What I mean is that if for example the interval is 5 sec and at 3 sec you move to the next slide, then that one will stay only for 2 sec instead of 5. There are no tests for this, but I think this is the expected behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I'll get that back in there then. Good catch.
Thanks
Josh Kurz (mobile)