Jump to content
Search Community

TimelineMax remove : further details please

Thomas James Thorstensson 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

Hello There. Hows it going?

 

 

In a banner project I'm working on I see this used where 'tl' is an instance of TimelineMax

tl.remove(tl.getChildren()[tl.getChildren().length-1])

Hmmmmm. That is slightly different to the api explanation:

 

.remove()
.remove( value:* ) : *

Removes a tween, timeline, callback, or label (or array of them) from the timeline.

 

Hmm....

 

Question, what can the previous coder be up to by the first line of code. Can you feed it multiple arguments?

 

Hmmm...not sure where I am, is this the way to the coffe machine?

 

 

Thankful for reply

 

 

.t

Link to comment
Share on other sites

It's removing the last child. The syntax is probably throwing you off, but there's only 1 argument there. This is what it's doing...

var children = tl.getChildren(); // Returns an array
var i = children.length - 1; // Index of last child in array
var lastChild = children[i]; // Set variable to last child in children array

tl.remove(lastChild);

The user called getChildren() the second time just to get the length of the array. Probably not the most efficient way to do it, but it works.

  • Like 2
Link to comment
Share on other sites

Thanks for your swift reply,

 

yes I understand that that will fetch the last child in the array. But what I don't understand is why he first fetches the entire array, then fetches the last child.

 

I mean, there are two calls to getChildren():

tl.remove(tl.getChildren()[tl.getChildren().length-1])

I am not sure what is happening under the hood: Is tl.remove first removing based on tl.getChildren() - and then a second time removing based on [tl.getChildren().length-1]

 

Many Thanks

 

.t

Link to comment
Share on other sites

You're right about it being unnecessary (wasteful in fact) to call getChildren() twice like that. Blake did a great job of explaining what that line was doing and that it wasn't two parameters. It's not very readable (the original, not Blake's). The original could be simplified to:

tl.remove( tl.getChildren().pop() );

That just grabs the children as an array and pop() will grab just the last one from that array, using it as the parameter for remove(). 

 

Does that clear things up? 

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