Jump to content
Search Community

Remove a TimelineMax when component destroyed

Thomas Miller test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hey!


I'm trying to delete a TimelineMax, once the user changes pages. I'm actually using Vue, so basically when a component gets destroyed, I want to totally remove the animations in that component.

 

However, I'm having a hard time completely removing it. It never actually gets destroyed, I'm doing something wrong.

 

Here is the pseudo-code I'm using (without the Vue stuff..):

 

// when omponent created, execute animation
animate(true);
// when component destroyed
animate(false);

function animate(x) {
  let tl = new TimelineMax({ repeat: -1 });
  // elements that I will animate
  const item1 = document.querySelector('.item1');
  const item2 = document.querySelector('.item2');
  const item3 = document.querySelector('.item3');

  // sub-timeline
  function anim(element) {
    const item = element;
    const subtimeline = new TimelineMax();
    // do the animations for one element
    subtimeline.add(() => { console.log('animating'); }, '+=0');
    subtimeline.to(item, 3, { x: '20', ease: Power4.easeInOut }, '+=0');
    // lots of animations..
    return subtimeline;
  }
  if (!x) {
    // I've tried lots of variants,
    // none of these work
    // I see the 'destroy' in the console when component is removed, but I still see the 'animating' as well, indefinitely..
    console.log('destroy');
    tl.invalidate();
    tl.invalidate().pause();
    tl.kill();
    tl = null;
  } else {
    tl.add(anim(item1), 0);
    tl.add(anim(item2), '+=1');
    tl.add(anim(item3), '+=1');
  }
}

 

Link to comment
Share on other sites

When you kill() an animation, it definitely won't keep playing.

 

It looks like the problem is that you're declaring your "tl" as a local variable inside animate() but then you're expecting it to persist. So when your component is created, you call animate(true) which works fine. But then when the component is destroyed and you call animate(false), inside that function you're creating ANOTHER "tl" timeline...and immediately killing it. You're not killing the original timeline you created when the component was created. See what I mean? 

 

So you'd need to store a reference to that timeline somewhere so that you can kill() it when you need to. 

 

Make sense? 

  • Like 4
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...