Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't cycle carousel when the page isn't visible #15566

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion js/carousel.js
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