Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Jonathan

Accessing the time property via the ticker methods event object

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!

Share this post


Link to post
Share on other sites

looks like after doing some tests.. it looks like that both output the same time. But i guess im unclear on which one is better to use?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

That's awesome Jack.. I didn't know you could store the ticker in a variable! Great idea.. this way i don't have to worry about garbage collection. Thanks for the quick solution!

Share this post


Link to post
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!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×