Creating a functional timer or countdown in After Effects can be frustrating if you try to do it manually. Keyframing every single second is not a viable option for professional work. Whether you are editing a fitness video, setting up a live stream countdown, or designing a HUD for a sci-fi scene, you need an automated solution.
This guide provides you with the exact expression code to build a dynamic timer that displays hours, minutes, and seconds. You will be able to control the entire timing animation with a single slider, making it fully customizable for any duration.
Setting Up Your Composition
Start by creating a clean workspace. Open After Effects and create a New Composition (Ctrl+N). For most digital content, a standard 1920x1080 resolution works best.
If you know your timer needs to run for a specific length, such as exactly one hour, you can set the composition Duration to 3600 seconds (1 hour) or 30 minutes depending on your project needs. Do not worry if you get this wrong initially; you can always change the duration later in the Composition Settings.
Adding and Formatting the Text
Select the Type Tool (Ctrl+T) and click anywhere in your composition. Type a placeholder format like 00:00:00 to represent hours, minutes, and seconds.
Pro Tip: This is a critical step often missed by beginners. You should use a Monospaced Font (like Courier New, Roboto Mono, or JetBrains Mono) for your timer. In standard fonts, the character width changes (the number 1 is thinner than 0), causing your entire timer to jitter or shake as the numbers change. A monospaced font keeps the text perfectly stable.
Once your text is typed, center it using the Align panel. If you plan to place a background box behind this text, you might want to check our guide on auto-resizing text boxes in After Effects to automate that process as well.
Applying the Slider Control
We will not animate the text layer directly. Instead, we will use a controller to drive the numbers.
Go to the Effects & Presets panel (usually on the right side, or press Ctrl+5). Search for Slider Control. Drag and drop this effect onto your text layer. This slider will act as the brain of your timer; the value you put here represents the total seconds.
The After Effects Timer Expression
This is the most important part. We need to connect the text to the slider using a JavaScript expression.
- Expand your text layer properties in the timeline.
- Find the Source Text property.
- Hold down the Alt key (Option on Mac) and click the stopwatch icon next to Source Text.
- A text editor box will open in the timeline. Copy and paste the following code exactly:
slider = Math.round(effect("Slider Control")("Slider"));
sec = slider % 60;
x = Math.floor(slider / 60);
min = x % 60;
hour = Math.floor(slider / 3600);
function addZero(n) {
if (n < 10) return "0" + n;
else return n;
}
addZero(hour) + ":" + addZero(min) + ":" + addZero(sec);This code takes the raw number from your Slider Control (which represents total seconds) and mathematically converts it into a readable HH:MM:SS format. It also adds a leading zero to single digits (so you see 09 instead of 9).
Troubleshooting: The Expression Error
If you paste the code and immediately see an orange banner error stating Expression Error, do not panic. This usually happens because of a difference in the JavaScript engine settings in older versions of After Effects.
To fix this, go to File > Project Settings > Expressions. Change the Expression Engine from JavaScript to Legacy ExtendScript. Click OK, and your timer should work instantly.
How to Animate the Timer
Now that the code is working, you control the time solely through the Slider Control effect in the Effects Controls panel.
Creating a Countdown (e.g., 60 Minutes to 0)
To make a countdown, you simply animate the value from high to low.
- Move your playhead to the start of the timeline.
- Set the Slider Control value to 3600 (60 minutes x 60 seconds). Click the stopwatch to add a keyframe.
- Move the playhead to the end of your desired duration.
- Set the value to 0.
Creating a Stopwatch (Count Up)
To count up, reverse the process. Start the slider at 0 and animate it to your target number (e.g., 600 for 10 minutes).
You can speed up or slow down the flow of time by moving the keyframes closer together or further apart. If you want a 30-minute timer to complete in just 10 seconds (for a time-lapse effect), simply place your start and end keyframes 10 seconds apart.
Styling Your Timer
Once the functionality is set, you can style the text just like any other layer. You can add a highlight effect to make the numbers glow or change color as time runs out.
For fitness videos or presentations, you often need the timer to be distinct but not distracting. Using a bold, high-contrast color helps readability. If you are building this as part of a larger motion graphics package, consider saving this text layer as an Animation Preset so you can drag and drop it into future projects without rewriting the code.
Comments (0)
Sign in to comment
Report