Skip to content

Commit

Permalink
Don't cycle carousel when page isn't visible
Browse files Browse the repository at this point in the history
Improves performance when the page is in a hidden tab or minimized window, etc.
Fixes #15298 as this happens to work around it by preventing the problematic situation from occurring.
  • Loading branch information
cvrebert committed Jan 14, 2015
1 parent fb6622a commit 519361a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion js/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@

this.options.interval
&& !this.paused
&& (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
&& (this.interval = setInterval(
// Make use of Page Visibility API when it's available
$.proxy(document.visibilityState ? this.nextWhenVisible : this.next, this),
this.options.interval
))

return this
}
Expand Down Expand Up @@ -104,6 +108,15 @@
return this
}

Carousel.prototype.nextWhenVisible = function () {
// Don't advance when the page isn't visible to the user.
// Saves the browser from doing unnecessary work and gives better UX.
// Also works around a Chrome bug: https://github.com/twbs/bootstrap/issues/15298
if (!document.hidden) {
this.next()
}
}

Carousel.prototype.next = function () {
if (this.sliding) return
return this.slide('next')
Expand Down

0 comments on commit 519361a

Please sign in to comment.