beyonk-group / svelte-carousel

A super lightweight, super simple Carousel for Svelte 3
MIT License
213 stars 38 forks source link

Fix error if calling `resume` method when carousel is already playing #57

Closed ed-sa-ma closed 3 years ago

ed-sa-ma commented 3 years ago

Bug description

When the component moves automatically via autoplay prop, there is the possibility to play/pause that movement via JavaScript with carouselRef.pause() and carouselRef.resume() methods.

If resume is called when the carousel is already playing, the component moves by 2 slides instead of just one. If the method gets called again, the movement increases to three slides each time.

Here is an example of it: https://svelte.dev/repl/f4e6f8863c3c47f4b64aa2ba9f32795d?version=3.32.0

Ideally, a call to resume when the component is already auto-playing should have no effect at all.

Technical fix

The current implementation of resume just adds and additional interval as long as autoplay prop is passed:

if (autoplay) {
  timer = setInterval(right, autoplay);
}

This PR just extends that condition to avoid adding a new interval if there is already one active.

antony commented 3 years ago

Thanks!