Jump to content
Search Community

Stop infinite loop, then display text doesn't problems

Guest
Moderator Tag

Recommended Posts

Hello, I'm new to GSAP, so for you my problem can be just stupid but I spent 2 days trying to solve this.
So I want to create a Spinner animation, that stops when backend responses and then the text is being displayed. But, at the beginning, I can't stop the animation on state change. My last attemt code is there:
 

  const spinnerTl = gsap.timeline({ repeat: -1 });
  console.log(isLoading);

  useEffect(() => {
    if (formik.isSubmitting) {
      const anim = document.getElementById("loader-animation");
      spinnerTl.to(anim, {
        rotation: "+=90",
        ease: "none",
        duration: 0.25,
        delay: 0.3,
      });
    }
  }, [formik.isSubmitting]);

  useEffect(() => {
    if (formik.isSubmitting && !isLoading) {
      const text = document.getElementById("loader-text");

      spinnerTl.willStop = true;
      let repeats = Math.floor(spinnerTl.totalTime() / spinnerTl.duration());
      console.log(Math.floor(spinnerTl.totalTime()));
      console.log(spinnerTl.duration());

      repeats += 1;
      console.log(spinnerTl);

      spinnerTl.repeat(0);

      const tl = gsap.timeline();
      gsap.set(text, { autoAlpha: 0 });
      tl.to(text, { delay: 0, autoAlpha: 1 });
    }
  }, [isLoading, formik.isSubmitting]);

You know, user submits the form, animation displays, on response he get text e.g. "it's ok, go to homepage". I found this solution on gsap forum, but for me totalTime() is always 0 so I tried to set spinnerTl.repeat(repeats), but repeats were always NaN, I tried setting repeat to 0 but it doesn't even change anything for my spinner timeline. i tried
onComplete: () => { isLoading && spinnerTl.pause() };

but it doesn't work too. Could anyone can help and explain what I do wrong?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...