Skip to content

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

@manutiedra

Description

@manutiedra

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;
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions