Course contentsShow
← Web Development
Lesson 2556 of 2,98246. Animation LibrariesFree lesson

Callback Functions

Use onStart, onUpdate, onComplete, and onRepeat callbacks to trigger logic during animations.

Callback Functions in GSAP

You'll learn how to execute custom logic at key moments during a GSAP animation using callback functions.

When animating with GSAP, you often need to trigger specific actions at different points in the animation's lifecycle—maybe start a sound effect when the animation begins, update a score counter as it progresses, or navigate to a new page when it completes. GSAP provides four essential callback functions for precisely this purpose.

The Four Animation Callbacks

onStart fires once when the animation begins (after any delay). Perfect for initializing states or logging events.

onUpdate fires on every animation frame while the animation is active. Use it for real-time tracking or synchronized effects.

onComplete fires once when the animation finishes. Ideal for triggering subsequent actions or cleanup.

onRepeat fires each time the animation repeats (if you set repeat greater than 0). Great for cyclical updates.

How to Use Them

Add callbacks directly to your animation configuration:

gsap.to(".box", {
  x: 400,
  duration: 2,
  repeat: 1,
  onStart: () => {
    console.log("Animation started!");
  },
  onUpdate: () => {
    console.log("Animating...");
  },
  onRepeat: () => {
    console.log("Repeating now");
  },
  onComplete: () => {
    console.log("All done!");
  }
});

Think of It Like a Cooking Timer

Callbacks are like notifications on a timer: onStart is "oven preheated," onUpdate is checking temperature every minute, onRepeat is "next batch ready," and onComplete is "timer finished—time to eat!"

Key Takeaway: GSAP's onStart, onUpdate, onComplete, and onRepeat callbacks let you hook into specific moments of an animation's lifecycle to trigger custom logic precisely when needed.