Skip to main content

gsap.getById()

Returns : Tween or Timeline

The tween or timeline associated with the given ID. Returns undefined if no tween or timeline has that ID.

Details

When creating a tween or timeline you can assign it an id so that you can reference it later. This is helpful when using frameworks and build tools like React where keeping track of variables can be difficult.

gsap.to(obj, { id: "myTween", duration: 1, x: 100 });

//later
let tween = gsap.getById("myTween"); //returns the tween
tween.pause();

Important: GSAP automatically releases animations for garbage collection shortly after they complete, so getById() will only find animations that are active or haven't begun yet. Otherwise, if kept all animations around just in case you're gonna call getById() to find one, it could quickly clog up the system and lead to memory leaks. If you need to maintain a reference to an animation even after it completes, you should use a variable like this:

let myTween = gsap.to(obj, { duration: 1, x: 100 });

// later
myTween.pause();