Autoplay timeleft restart issue #7418
-
Check that this is really a bug
Reproduction linkhttps://codesandbox.io/p/sandbox/y29vdk?file=/src/App.jsx Bug descriptionI've been using your Autoplay demo code for React Swiper and have a problem with the timeleft restart. Here is my code: const progressCircle = useRef(null); const [controlledSwiperMain, setControlledSwiperMain] = useState(null); const onAutoplayTimeLeft = (s, time, progress) => { useEffect(() => { Here is my Swiper config:
Expected BehaviorWhat I want: I want to create a feature when my Swiper container is visible in the viewport to make the autoplay ON but when it's not make it OFF because it's laggy. I need a way to restart timeleft when I call a swiperContainer.autoplay.stop(); because it doesn't restart itself by default... Actual BehaviorWhat I get: On the first render it's counting 3 sec and I get normal expected behavior but after I scroll down out of the viewport and return, it seems like the count was still counting downwards and never stopped nor got restarted automatically, and that negative time reflected on my progress circle for example if I didn't return to the section for 10 sec the progress circle will show -10s even though the autoplay stopped sliding slides which means my IntersectionObserver works fine and the issue is not related to it! Swiper version"swiper": "^10.3.1" Platform/Target and Browser VersionsWindows 10 Validations
Would you like to open a PR for this bug?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Okay, I fixed it. Since stop never restarts time we start it initially and then just resume or stop based on entry.isIntersecting flag. In case you encounter this issue this is a 100% working solution: useEffect(() => { |
Beta Was this translation helpful? Give feedback.
Okay, I fixed it. Since stop never restarts time we start it initially and then just resume or stop based on entry.isIntersecting flag.
In case you encounter this issue this is a 100% working solution:
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (swiperContainer) {
if (!swiperContainer.running) {
swiperContainer.autoplay.start();
}
swiperContainer.autoplay[entry.isIntersecting ? 'resume' : 'stop']();
}
},
{ threshold: 0.5 },
);
observer.observe(sectionRef.current);
return () => {
observer.unobserve(sectionRef.current);
};
}, [swiperContainer]);