May 22, 2013

After Effects Tutorial – Building an Automated Timer

After Effects, Freebies, Tutorials, Video Post by

Today I had to make a timer in After Effects that automatically counted up in milliseconds starting from the time it was placed in to the timeline. I also needed it to freeze on the 60 seconds mark. With a bit of searching the web as well as the help of expert Programmer "Mambose" we managed to come up with a simple expression that executes the task perfectly. In addition to that, you can easily adjust the text that displays at any specified time (I've underlined those editable areas, for this video I made it stop at 30 seconds).

In order to add the code. Create a new blank text layer, click on the "Text" drop-down in the composition and the Alt-click on the "Source Text" stopwatch. Paste the code in the right column as seen in this screenshot.

Here's the code:

countspeed = 1; clockStart = 0;
function times(n){
if (n < 10) return "0" + n else return "" + n
}

clockTime = clockStart +countspeed*(time - inPoint);

if (clockTime < 0){
minus = "-";
clockTime = -clockTime;
}else{
minus = "";
}

t = Math.floor(clockTime);
h = Math.floor(t/3600);
min = Math.floor((t%3600)/60);
sec = Math.floor(t%60);
ms = clockTime.toFixed(2).substr(-2);
if (clockTime>30) {
"DING!"
} else {
minus + times(h) + ":" + times(min) + ":" + times(sec) + "." + ms
}


EDIT: in the comments someone asked how to make this in to a countdown timer. You can find my response below or you could use the following code...

Here's the code:

countspeed = -1; clockStart =20; clockStop =0;
function times(n){
if (n < 10) return "0" + n else return "" + n
}

clockTime = clockStart +countspeed*(time - inPoint);

if (clockTime < 0){
minus = "";
clockTime = -clockTime;
}else{
minus = "";
}

t = Math.floor(clockTime);
h = Math.floor(t/3600);
min = Math.floor((t%3600)/60);
sec = Math.floor(t%60);
ms = clockTime.toFixed(2).substr(-2);
if (clockTime < clockStop) {
"DING!"
} else {
minus + times(h) + ":" + times(min) + ":" + times(sec) + "." + ms
}


I hope you enjoy!

After Effects, Freebies, Tutorials, Video post by .

Share Post

6 comments on “After Effects Tutorial – Building an Automated Timer

  1. this would be really usefull if only one could copy and paste the code….

  2. ooops, soooo sorry, I was trying to copy and paste to desktop – did not realise you could do a direct paste! THANKS SO MUCH FOR THIS CLOCK!

  3. Addeyry on said:

    Thank you so much for this code!!!

    I modyfieid it a bit to count 4 minutes every second and start at 95 hours. For some reason it wasn’t counting past 100 but if you added a new time unit without defining it it counted past 100. I used it in my school project if that’s ok.

  4. How would I reverse this into a countdown? Thanks ahead.

    • Darren on said:

      I’m no coder but after playing around I figured out how to do it. To set your countdown time change the 20 to your desired number in the first line.

      countspeed = -1; clockStart =20; clockStop =0;
      function times(n){
      if (n < 10) return "0" + n else return "" + n
      }

      clockTime = clockStart +countspeed*(time - inPoint);

      if (clockTime < 0){
      minus = "";
      }else{
      minus = "";
      }

      t = Math.floor(clockTime);
      h = Math.floor(t/3600);
      min = Math.floor((t%3600)/60);
      sec = Math.floor(t%60);
      ms = clockTime.toFixed(2).substr(-2);
      if (clockTime < clockStop) {
      "DING!"
      } else {
      minus + times(h) + ":" + times(min) + ":" + times(sec) + "." + ms
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.