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

Reset the Interval #4

Closed
hamzaerbay opened this issue Aug 12, 2018 · 2 comments
Closed

Reset the Interval #4

hamzaerbay opened this issue Aug 12, 2018 · 2 comments

Comments

@hamzaerbay
Copy link

My opinion, if click on the next button (autoplay on), it should restart the interval. WDYT?

@DZuz14
Copy link
Owner

DZuz14 commented Aug 12, 2018

Yeah, you are totally right. I never thought of that. This is actually an interesting problem to solve, as the code definitely needs a refactoring to support this. Any ideas?

@DZuz14 DZuz14 closed this as completed Jan 31, 2020
@NicolaG1993
Copy link
Contributor

This is how I've solved this issue in my App:

(Slider.js)

  • delete play, interval, the if statement that activate setInterval, and the clearInterval as well
  • write them inside a new useEffect
  • inside the array pass activeSlide

All of this code now gets activated only when a new slide renders on screen, and this will also clear the interval when the component unmount

I haven't tested it so much yet and i'm new to React too, so im not 100% sure this is the best solution, but it seems to work!

Btw thanks a lot @DZuz14! I've founded this slider very usefull for my project, so I really hope i can give something back with this solution

here is the code:
useEffect(() => { const play = () => { autoPlayRef.current(); }; let interval = null; if (props.autoPlay) { interval = setInterval(play, props.autoPlay * 1000); } return () => { if (props.autoPlay) { clearInterval(interval); } }; }, [activeSlide]);

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

3 participants