Skip to content

Commit 519361a

Browse files
committed
Don't cycle carousel when page isn't visible
Improves performance when the page is in a hidden tab or minimized window, etc. Fixes twbs#15298 as this happens to work around it by preventing the problematic situation from occurring.
1 parent fb6622a commit 519361a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

js/carousel.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959

6060
this.options.interval
6161
&& !this.paused
62-
&& (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
62+
&& (this.interval = setInterval(
63+
// Make use of Page Visibility API when it's available
64+
$.proxy(document.visibilityState ? this.nextWhenVisible : this.next, this),
65+
this.options.interval
66+
))
6367

6468
return this
6569
}
@@ -104,6 +108,15 @@
104108
return this
105109
}
106110

111+
Carousel.prototype.nextWhenVisible = function () {
112+
// Don't advance when the page isn't visible to the user.
113+
// Saves the browser from doing unnecessary work and gives better UX.
114+
// Also works around a Chrome bug: https://github.com/twbs/bootstrap/issues/15298
115+
if (!document.hidden) {
116+
this.next()
117+
}
118+
}
119+
107120
Carousel.prototype.next = function () {
108121
if (this.sliding) return
109122
return this.slide('next')

0 commit comments

Comments
 (0)