简体   繁体   English

gsap tweenlite / tweenmax垃圾收集,参考和表演

[英]gsap tweenlite/tweenmax garbage collecting, references and performances

I'm trying to understand what's the best way to use TweenLite/TweenMax. 我试图了解使用TweenLite / TweenMax的最佳方法是什么。

  • Is it useful to reference all my tweens with the same variable? 使用相同的变量引用所有补间是否有用?
  • After killing the tween with the relative public method, do I have to set the reference to null to improve the garbage collecting disposal? 在使用相对公共方法杀死补间之后,我是否必须将引用设置为null以改进垃圾收集处理?

Below there is a well commented example: 下面是一个评论很好的例子:

$(document).ready(function () {
    var elementOne = $('#elementOne');
    var elementTwo = $('#elementTwo');
    var myTween;

    // is it useful to overwrite the variable?
    myTween = TweenMax.to(elementOne, 1, {
        opacity: 0
    });
    myTween = TweenMax.to(elementTwo, 1, {
        left: 0,
        onComplete: destroy
    });

    function destroy () {
        // suggested on tweenmax docs
        // the console.log still returns me the object
        myTween.kill();
        console.log(myTween);

        // is it required for garbage collecting?
        // now the console.log returns me null
        myTween = null;
        console.log(myTween);

        // and then...jQuery GC friendly remove
        elementOne.remove();
        elementTwo.remove();
    }
});

You don't need to do anything special to make a tween (or timeline) available for gc other than what you'd normally do for any JS object. 除了通常对任何JS对象执行的操作之外,您不需要做任何特殊的事情来使gc(或时间轴)可用于gc。 In other words, if you maintain a reference in your own code to an instance, it'll stick around (otherwise your code could break). 换句话说,如果你在自己的代码中维护一个实例的引用,它就会一直存在(否则你的代码就会崩溃)。 But you do NOT need to specifically kill() a tween. 但你并不需要专门杀()补间。 A lot of effort has gone into GSAP to ensure that things are optimized and headache-free. GSAP已经付出了很多努力来确保事情得到优化和无头痛。 The engine will automatically release completed tweens for garbage collection when necessary. 必要时,引擎将自动释放已完成的补间以进行垃圾回收。 And yet a tween will still work if you maintain a reference and restart() it, for example. 但是,如果你维护一个引用并重新启动()它,那么补间仍然可以工作。

Just because you call kill() on a tween instance, that doesn't force the browser to run its garbage collection routine. 仅仅因为你在补间实例上调用kill(),这并没有强制浏览器运行它的垃圾收集例程。 It doesn't null your variable either. 它也不会使您的变量为空。 That's just how JavaScript works (and that's a good thing). 这就是JavaScript的工作方式(这是一件好事)。 It has nothing to do with TweenLite/Max specifically. 它与TweenLite / Max无关。

Also keep in mind that you don't need to store any tween instances in variables. 还要记住,您不需要将任何补间实例存储在变量中。 The only time it's helpful is if you need to control the tween later (or insert it into a timeline or something like that). 唯一有用的是,如果您需要稍后控制补间(或将其插入时间轴或类似的东西)。 Typically it's fine to just call TweenMax.to(...) without storing the result in a variable. 通常,只需调用TweenMax.to(...)而不将结果存储在变量中即可。

Does that clear things up? 那清楚了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM