Skip to content

Carousel doesn't stop if calling cycle several times before calling pause #1077

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

Open
manutiedra opened this issue Jun 1, 2025 · 1 comment

Comments

@manutiedra
Copy link

Describe the bug
If you have a carousel and call several times the cycle method, then calling the pause method does not work

Expected behavior
it doesn't matter how many time you call cycle. If after a few calls to cycle you call pause, the carousel should pause

Additional context
The cycle method is:

/**
 * Set an interval to cycle through the carousel items
 */
Carousel.prototype.cycle = function () {
    var _this = this;
    if (typeof window !== 'undefined') {
        this._intervalInstance = window.setInterval(function () {
            _this.next();
        }, this._intervalDuration);
    }
};

As you can see, it doesn't check if it already had a called setInterval before, so calling it several times will make impossible to stop the timer. The proper fix can be something like this:

/**
 * Set an interval to cycle through the carousel items
 */
Carousel.prototype.cycle = function () {
    var _this = this;

    if (this._intervalInstance) {
        clearInterval(this._intervalInstance);
        this._intervalInstance = null;
    }

    if (typeof window !== 'undefined') {
        this._intervalInstance = window.setInterval(function () {
            _this.next();
        }, this._intervalDuration);
    }
};

Please fix it

/**
 * Clears the cycling interval
 */
Carousel.prototype.pause = function () {
    if (this._intervalInstance) {
        clearInterval(this._intervalInstance);
        this._intervalInstance = null;
    }
};
@manutiedra
Copy link
Author

Also, it would be good to call clearInterval in the destroy method or call to pause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant