Jump to content
Search Community

Accessing the time property via the ticker methods event object

Jonathan 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

I am accessing the time property via the ticker() methods event object:

TweenMax.ticker.addEventListener("tick", loop, this, true, 1);

function loop(e){
 
       // the time property in the target
       if(window.console) console.log(e.target.time);
       // the time property in the target._eventTarget 
       if(window.console) console.log(e.target._eventTarget.time);

       // do stuff with passing time
}

When accessing the time property in the event object that gets passed, which time property is best to use when keeping track of the passed time:

 

e.target.time or e.target._eventTarget.time

 

Any help will be highly appreciated!

Link to comment
Share on other sites

They refer to exactly the same object :) Just so you know, anything that begins with an underscore, like _eventTarget is typically intended for internal use, not public. 

 

My advice would actually be to use neither of those options - I'd just reference the ticker's time directly and not have your event listener request event objects to be passed as parameters to your handler because those end up needing to get garbage collected. It isn't wrong or anything - I just feel that it's wasteful in this context. If you've got lots of Tickers that you're using, it may come in handy to know which one called the handler, but it's very rare.

 

Instead, I'd just do this:

var ticker = TweenMax.ticker;
ticker.addEventListener("tick", loop, this, false, 1);
function loop(){
       if (window.console) console.log(ticker.time);
}

Notice I created a variable that you can use closure-style in your function - that's not necessary, but it's just a slight optimization (fewer lookups). 

 

I also set the useParams parameter to false so that no event objects are passed to your handler (less gc). 

  • Like 1
Link to comment
Share on other sites

one last thing.. if for some reason I have to use multiple tickers, to know which one called the ticker, and use the event object to keep track using the scope parameter. If I use one of the kill methods, does that remove any garbage, regarding the ticker?

 

or in general when killing a tween, timeline, delayedCallback, or removing the listener from a ticker.. does that remove garbage?

 

thanks again for the help!

Link to comment
Share on other sites

I didn't quite understand your question. The kill() methods of the tweening classes are completely unrelated to the ticker. And there's nothing you can do to force a garbage collection sweep - that's totally up to the browser. All you can do is make sure you don't keep references to things or spew out wasteful objects. 

 

And yes, you should removeEventListener() if you don't need it anymore. 

 

Generally you shouldn't have to worry much about gc in GSAP. But I would recommend against setting the "userParam" to true in your addEventListener() call if you can possibly avoid it, just so you're not having it generate 60 extra objects per second. You'd probably be fine, but I'm just an optimization freak. 

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