How to Make a Timer in Scratch

What is a timer in Scratch?

Timers are a component of a program or game that measures time in specific intervals. 

A timer can be used to delay actions, limit the duration of actions, or allow actions to continue for a total amount of time.

You can add a timer to count up or count down. Timers can be used in various projects, from simple games to complex animations. Each project may require a different type of timer.

In Scratch, timers are used to control the execution of command blocks or schedule actions to occur at specific intervals. 

Timers require a trigger to start or stop. For example, your timer may start when the green flag is clicked and stop once it reaches a certain amount of time. Or maybe your timer starts with a new level and stops when the sprite is clicked.

Timers in Scratch can be created using custom variables or the built-in timer variable.

Custom variable

You can create a timer in Scratch by creating a custom variable to store the time.

create a custom variable in Scratch

Built-in variable

The timer variable is a feature in Scratch that records how many seconds have passed since the last time the green flag was clicked or the timer was reset.

Built-in timer variable

Why add a timer? 

Timers are a way to make your game or project more dynamic and engaging.

In a game, a timer can present a challenge. Let’s say you only have 5 minutes to complete a level, this helps structure the game and adds difficulty.

For apps, a timer may be used to track goals. Think of a running app where you track the number of miles you can in one day.

There are a variety of ways timers are useful in a project or game including:

Scheduling actions

Timers can schedule specific actions to happen at predetermined intervals, such as executing code or triggering an event.

Breaking down tasks

Timers help break down challenging tasks into smaller, more manageable chunks. This prevents users from getting overwhelmed and maintain a sense of progress.

Adding stakes

Timers add stakes to a game such as by making players lose if they run out of time. For example, players could try to get as many points as possible or solve a puzzle before the timer runs out.

Monitoring performance

Timers can be used to monitor the performance of code, such as measuring how long it takes to run.

How to Make a Countdown Timer in Scratch

A countdown timer starts at a certain number and counts down, usually ending at 0.

In this Flying Space Cat game, you can make the game more difficult and fun! by limiting the amount of time players have to accomplish the goal of collecting crystals.

Remix this project to add a timer to it.

Let’s add a countdown timer to limit the players to 60 seconds.

  1. Start the timer
  2. Countdown every 1 second
  3. Add the next action
  4. Customize your timer

Step 1 Start the timer

We will set the timer to start when the game starts, when we click the green flag.

  • Add a “When Green Flag Clicked” block to your sprite.
when green flag clicked block

The time starts at 60 seconds. Since the time changes, use a variable to hold this value.

  • In the variables blocks, make a variable called Time and check it off
  • Under the “When Green Flag Clicked” block, add a set variable block. Update the dropdown to your Time variable and enter a number of seconds to start your timer. Here we are using 60.
make a variable block

Press the green flag to see the time displayed.

Step 1 start scratch timer

Step 2 Countdown every 1 second

Every second, the time decreases by 1 until it reaches 0. Then the game is over.

  • Add a “repeat until” loop with an operator for “Time < 1”.
  • Inside the loop, add a “wait 1 seconds” block
  • Below that, add a “change Time by -1”
repeat loop

Click the green flag to watch your timer countdown!

Step 2 timer counts down

Step 3 Add the next action

When the time runs out, or reaches 0, the loop and the game ends.

Decide what happens when the time runs out. In our game, the “Game Over” message displays.

  • After the loop, add a “Broadcast game over” block.
broadcast game over
completed time block

Run down the time to see the Game Over message.

Step 3 game over message

Your timer is complete.

Step 4 Customize your timer

You can easily customize this timer by changing these features:

How long is the countdown?

Change the 60 in “Set Time to” block to another number.

What happens in your app after the time runs out? 

Add that code beneath the “Repeat Until” block.

How fast does the timer countdown? 

Change the time in “wait 1 seconds” and “change Time by -1” to countdown faster or slower.

Make a Stopwatch in Scratch

We can also create a stopwatch in Scratch. A stopwatch measures the time that has elapsed from when it’s started. It’s often used when we need precise time measurements like in a race where only seconds separate the winners and losers.

Scratch has a built-in timer variable that tracks to the microsecond how much time has passed since the last time the green flag was clicked or the timer was reset.

I’ll show you how to start and stop a timer here.

Add a Stopwatch To “A Unicorn Sighting”

You are hanging out at the beach and see an incredible sight, a white unicorn running across the beach! Build a timer to track how long your unicorn sighting is, it will be different every time. Start the timer when you see the unicorn and stop it when it disappears.

Remix this project to add a stopwatch to it. You can also see the completed A Unicorn Sighting.

Now, let’s add a stopwatch to track the time.

  1. Create a button sprite
  2. Set the stopwatch to 0 to start
  3. Change the button when it’s clicked
  4. Control the start and stop button

Step 1 Create a button sprite

We need one sprite, this is a start and stop button.

  • Click the “Choose a Sprite” button. I’ve customized the Button2 sprite in the Scratch library.
Choose a sprite
  • Create two costumes called start and stop.
create a start and stop costume
completed start and stop costume in scratch

You should see the button in your preview screen!

Step 2 Set the stopwatch to 0 to start

Add this code to your button sprite. We will set the stopwatch to 0 when the application is started.

  • Add a “When Green Flag Clicked” block to your sprite.
when green flag clicked

Since the time changes, use a variable to hold this value.

  • In the variables blocks, make a variable called time and check it off.
make a time variable in scratch
  • On the preview screen, double-click the time display to show a large readout and move it near your button.
  • Under the “When Green Flag Clicked” block, add a “set time to” block and use 0.
  • Add “switch costume to” block and start with the Start button

Press the green flag to see the stopwatch set to “start” and time variable to 0. Make sure you are seeing the right button and time.

set time and button

Step 3 Change the button when it’s clicked

When the start button is clicked, it will change to stop and vice versa.

  • Control this with a “When this sprite clicked” block
  • Change the costume using “next costume”
next costume block

Try clicking on the sprite, the costume should change when you click it.

step 3 start and stop button

Step 4 Control the start and stop button

Now, we need to control the time using the start and stop buttons.

Create two if statements based on what the button is showing. The first if statement is when the start button is clicked.

  • Add an if then block
  • Use the condition “costume name” equals stop (remember this is the costume that shows after the button is clicked)
  • Inside the if then, add a reset timer
  • Use a repeat until loop with the condition “costume name” equals stop
  • Inside the loop, set the time variable equal to the timer variable
if then block
start costume if then code block

The second if statement is when the stop button is clicked.

  • Add an if then block
  • Use the condition “costume name” equals start
  • Inside the if then, show the variable time (this is the time when the button is pressed)
stop if then code block
completed stop if then code block
Step 4 start and stop loop

Click the start button and see if it works! Watch the unicorn run across the screen and capture the time.

Now, you have a simple stopwatch to track the time.

Try creating your own stopwatch or adding one to your next project. There are plenty of ways to customize it and improve on this simple stopwatch.

1 Comment

  • Mayowa Alasa April 3, 2025

    Thank you for this!

Comments are closed.