themesberg / themesberg/flowbite

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

Open
#1,077 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
9.4k
Forks
862
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating Carousel.prototype.cycle and Carousel.prototype.pause, then reproduce the issue by calling cycle several times before pause. Done means repeated cycle calls leave only one active interval and a subsequent pause stops carousel advancement.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.