Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more straightforward to make one than you might think and only requires the trifecta of HTML, CSS and JavaScript. Let’s make one together!
This is what we’re aiming for:
Here are a few things the timer does that we’ll be covering in this post:
- Displays the initial time remaining
- Converts the time value to a
MM:SS
format - Calculates the difference between the initial time remaining and how much time has passed
- Changes color as the time remaining nears zero
- Displays the progress of time remaining as an animated ring
OK, that’s what we want, so let’s make it happen!
Step 1: Start with the basic markup and styles
Let’s start with creating a basic template for our timer. We will add an svg with a circle element inside to draw a timer ring that will indicate the passing time and add a span to show the remaining time value. Note that we’re writing the HTML in JavaScript and injecting into the DOM by targeting the #app
element. Sure, we could move a lot of it into an HTML file, if that’s more your thing.
document.getElementById("app").innerHTML = `
<div class="base-timer">
<svg class="base-timer__svg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g class="base-timer__circle">
<circle class="base-timer__path-elapsed" cx="50" cy="50" r="45" />
</g>
</svg>
<span>
<!-- Remaining time label -->
</span>
</div>
`;
Now that we have some markup to work with, let’s style it up a bit so we have a good visual to start with. Specifically, we’re going to:
- Set the timer’s size
- Remove the fill and stroke from the circle wrapper element so we get the shape but let the elapsed time show through
- Set the ring’s width and color
/* Sets the containers height and width */
.base-timer {
position: relative;
height: 300px;
width: 300px;
}
/* Removes SVG styling that would hide the time label */
.base-timer__circle {
fill: none;
stroke: none;
}
/* The SVG path that displays the timer's progress */
.base-timer__path-elapsed {
stroke-width: 7px;
stroke: grey;
}
Having that done we end up with a basic template that looks like this.

Step 2: Setting up the time label
As you probably noticed, the template includes an empty <span> that’s going to hold the time remaining. We will fill that place with a proper value. We said earlier that the time will be in MM:SS
format. To do that we will create a method called formatTimeLeft
:
function formatTimeLeft(time) {
// The largest round integer less than or equal to the result of time divided being by 60.
const minutes = Math.floor(time / 60);
// Seconds are the remainder of the time divided by 60 (modulus operator)
let seconds = time % 60;
// If the value of seconds is less than 10, then display seconds with a leading zero
if (seconds < 10) {
seconds = `0${seconds}`;
}
// The output in MM:SS format
return `${minutes}:${seconds}`;
}
Then we will use our method in the template:
document.getElementById("app").innerHTML = `
<div class="base-timer">
<svg class="base-timer__svg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g class="base-timer__circle">
<circle class="base-timer__path-elapsed" cx="50" cy="50" r="45"></circle>
</g>
</svg>
<span id="base-timer-label" class="base-timer__label">
${formatTime(timeLeft)}
</span>
</div>
`
To show the value inside the ring we need to update our styles a bit.
.base-timer__label {
position: absolute;
/* Size should match the parent container */
width: 300px;
height: 300px;
/* Keep the label aligned to the top */
top: 0;
/* Create a flexible box that centers content vertically and horizontally */
display: flex;
align-items: center;
justify-content: center;
/* Sort of an arbitrary number; adjust to your liking */
font-size: 48px;
}
OK, we are ready to play with the timeLeft
value, but the value doesn’t exist yet. Let’s create it and set the initial value to our time limit.
// Start with an initial value of 20 seconds
const TIME_LIMIT = 20;
// Initially, no time has passed, but this will count up
// and subtract from the TIME_LIMIT
let timePassed = 0;
let timeLeft = TIME_LIMIT;
And we are one step closer.

Right on! Now we have a timer that starts at 20 seconds… but it doesn’t do any counting just yet. Let’s bring it to life so it counts down to zero seconds.
Step 3: Counting down
Let’s think about what we need to count down the time. Right now, we have a timeLimit
value that represents our initial time, and a timePassed
value that indicates how much time has passed once the countdown starts.
What we need to do is increase the value of timePassed
by one unit per second and recompute the timeLeft
value based on the new timePassed
value. We can achieve that using the setInterval
function.
Let’s implement a method called startTimer
that will:
- Set counter interval
- Increment the
timePassed
value each second - Recompute the new value of
timeLeft
- Update the label value in the template
We also need to keep the reference to that interval object to clear it when needed — that’s why we will create a timerInterval
variable.
let timerInterval = null;
document.getElementById("app").innerHTML = `...`
function startTimer() {
timerInterval = setInterval(() => {
// The amount of time passed increments by one
timePassed = timePassed += 1;
timeLeft = TIME_LIMIT - timePassed;
// The time left label is updated
document.getElementById("base-timer-label").innerHTML = formatTime(timeLeft);
}, 1000);
}
We have a method that starts the timer but we do not call it anywhere. Let’s start our timer immediately on load.
document.getElementById("app").innerHTML = `...`
startTimer();
That’s it! Our timer will now count down the time. While that’s great and all, it would be nicer if we could add some color to the ring around the time label and change the color at different time values.

Step 4: Cover the timer ring with another ring
To visualize time passing, we need to add a second layer to our ring that handles the animation. What we’re doing is essentially stacking a new green ring on top of the original gray ring so that the green ring animates to reveal the gray ring as time passes, like a progress bar.
Let’s first add a path element in our SVG element.
document.getElementById("app").innerHTML = `
<div class="base-timer">
<svg class="base-timer__svg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g class="base-timer__circle">
<circle class="base-timer__path-elapsed" cx="50" cy="50" r="45"></circle>
<path
id="base-timer-path-remaining"
stroke-dasharray="283"
class="base-timer__path-remaining ${remainingPathColor}"
d="
M 50, 50
m -45, 0
a 45,45 0 1,0 90,0
a 45,45 0 1,0 -90,0
"
></path>
</g>
</svg>
<span id="base-timer-label" class="base-timer__label">
${formatTime(timeLeft)}
</span>
</div>
`;
Next, let’s create an initial color for the remaining time path.
const COLOR_CODES = {
info: {
color: "green"
}
};
let remainingPathColor = COLOR_CODES.info.color;
Finally, let’s add few styles to make the circular path look like our original gray ring. The important thing here is to make sure the stroke-width
is the same size as the original ring and that the duration of the transition
is set to one second so that it animates smoothly and corresponds with the time remaining in the time label.
.base-timer__path-remaining {
/* Just as thick as the original ring */
stroke-width: 7px;
/* Rounds the line endings to create a seamless circle */
stroke-linecap: round;
/* Makes sure the animation starts at the top of the circle */
transform: rotate(90deg);
transform-origin: center;
/* One second aligns with the speed of the countdown timer */
transition: 1s linear all;
/* Allows the ring to change color when the color value updates */
stroke: currentColor;
}
.base-timer__svg {
/* Flips the svg and makes the animation to move left-to-right */
transform: scaleX(-1);
}
This will output a stroke that covers the timer ring like it should, but it doesn’t animate just yet to reveal the timer ring as time passes.

To animate the length of the remaining time line we are going to use the stroke-dasharray
property. Chris explains how it’s used to create the illusion of an element “drawing” itself. And there’s more detail about the property and examples of it in the CSS-Tricks almanac.
Step 5: Animate the progress ring
Let’s see how our ring will look like with different stroke-dasharray
values:

What we can see is that the value of stroke-dasharray
is actually cutting our remaining time ring into equal-length sections, where the length is the time remaining value. That is happening when we set the value of stroke-dasharray
to a single-digit number (i.e. 1-9).
The name dasharray suggests that we can set multiple values as an array. Let’s see how it will behave if we set two numbers instead of one; in this case, those values are 10 and 30.

stroke-dasharray: 10 30
That sets the first section (remaining time) length to 10 and the second section (passed time) to 30. We can use that in our timer with a little trick. What we need initially is for the ring to cover the full length of the circle, meaning the remaining time equals the length of our ring.
What’s that length? Get out your old geometry textbook, because we can calculate the length an arc with some math:
Length = 2πr = 2 * π * 45 = 282,6
That’s the value we want to use when the ring initially mounted. Let’s see how it looks.

stroke-dasharray: 283 283
That works!
OK, the first value in the array is our remaining time, and the second marks how much time has passed. What we need to do now is to manipulate the first value. Let’s see below what we can expect when we change the first value.

We will create two methods, one responsible for calculating what fraction of the initial time is left, and one responsible for calculating the stroke-dasharray
value and updating the <path>
element that represents our remaining time.
// Divides time left by the defined time limit.
function calculateTimeFraction() {
return timeLeft / TIME_LIMIT;
}
// Update the dasharray value as time passes, starting with 283
function setCircleDasharray() {
const circleDasharray = `${(
calculateTimeFraction() * FULL_DASH_ARRAY
).toFixed(0)} 283`;
document
.getElementById("base-timer-path-remaining")
.setAttribute("stroke-dasharray", circleDasharray);
}
We also need to update our path each second that passes. That means we need to call the newly created setCircleDasharray
method inside our timerInterval
.
function startTimer() {
timerInterval = setInterval(() => {
timePassed = timePassed += 1;
timeLeft = TIME_LIMIT - timePassed;
document.getElementById("base-timer-label").innerHTML = formatTime(timeLeft);
setCircleDasharray();
}, 1000);
}
Now we can see things moving!

Woohoo, it works… but… look closely, especially at the end. It looks like our animation is lagging by one second. When we reach 0 a small piece of the ring is still visible.

This is due to the animation’s duration being set to one second. When the value of remaining time is set to zero, it still takes one second to actually animate the ring to zero. We can get rid of that by reducing the length of the ring gradually during the countdown. We do that in our calculateTimeFraction
method.
function calculateTimeFraction() {
const rawTimeFraction = timeLeft / TIME_LIMIT;
return rawTimeFraction - (1 / TIME_LIMIT) * (1 - rawTimeFraction);
}
There we go!

Oops… there is one more thing. We said we wanted to change the color of the progress indicator when when the time remaining reaches certain points — sort of like letting the user know that time is almost up.
Step 6: Change the progress color at certain points of time
First, we need to add two thresholds that will indicate when we should change to the warning and alert states and add colors for each of that states. We’re starting with green, then go to orange as a warning, followed by red when time is nearly up.
// Warning occurs at 10s
const WARNING_THRESHOLD = 10;
// Alert occurs at 5s
const ALERT_THRESHOLD = 5;
const COLOR_CODES = {
info: {
color: "green"
},
warning: {
color: "orange",
threshold: WARNING_THRESHOLD
},
alert: {
color: "red",
threshold: ALERT_THRESHOLD
}
};
Now, let’s create a method that’s responsible for checking if the threshold exceeded and changing the progress color when that happens.
function setRemainingPathColor(timeLeft) {
const { alert, warning, info } = COLOR_CODES;
// If the remaining time is less than or equal to 5, remove the "warning" class and apply the "alert" class.
if (timeLeft <= alert.threshold) {
document
.getElementById("base-timer-path-remaining")
.classList.remove(warning.color);
document
.getElementById("base-timer-path-remaining")
.classList.add(alert.color);
// If the remaining time is less than or equal to 10, remove the base color and apply the "warning" class.
} else if (timeLeft <= warning.threshold) {
document
.getElementById("base-timer-path-remaining")
.classList.remove(info.color);
document
.getElementById("base-timer-path-remaining")
.classList.add(warning.color);
}
}
So, we’re basically removing one CSS class when the timer reaches a point and adding another one in its place. We’re going to need to define those classes.
.base-timer__path-remaining.green {
color: rgb(65, 184, 131);
}
.base-timer__path-remaining.orange {
color: orange;
}
.base-timer__path-remaining.red {
color: red;
}
Voilà, there we have it. Here’s the demo again with everything put together.
Related… Chen Hui Jing’s Can you make a countdown timer in pure CSS?
Here’s another quick pure CSS version based on the one in this article.
Changes:
Simplified the SVG by making both elements circles around the
0,0
point (setting theviewBox
to-50 -50 100 100
puts the origin0,0
dead in the middle of the SVG and we don’t need to setcx
andcy
anymore as they both default to0
)Animated both the
stroke-dashoffset
and thestroke
using alinear
keyframe animationsAnimated the timer content using a
steps()
keyframe animation (this part can be simplified a lot if we were to use Houdini, but then that would be at the expense of support, so I don’t know)It would be great if it would click a button or automatically go to a specific url when it finished.
How do you make the timer fill up instead? I want to sample from this example to make a small music player
You can replace
calculateTimeFraction() * FULL_DASH_ARRAY
withFULL_DASH_ARRAY - (calculateTimeFraction() * FULL_DASH_ARRAY)
.How do we change the direction for this?
Nice! I’ve been playing around with timers myself for a bit. Just a little thing if anyone is interested, you
can forgo the if statement to display the time <10 with a leading 0 by using padStart. Like this:
return
${minutes}:${seconds.toString().padStart(2, '0')}
;I thought this would be a good candidate for converting to a custom element, so I tried. I’m almost done, but what the heck:
https://codepen.io/amundo/pen/PoqYWEK
This was a really cool, well-written article!
There is still a small but in that the ‘ring’ doesn’t appear to start moving until the countdown reaches 00:19.
Is it possible to calculate timer in terms of working days?
I’d advise a little revision of the tutorial as right now there are some steps missing when following it – e.g. the condition from the startTimer() function (so the timer never stops and goes to negative values) or setting the FULL_DASH_ARRAY constant.
This count down clock is beautiful and perfect for a billiards game.
Would need to be able to start, stop/pause, resume in what I’ve seen called lags, each start and stop report the length of time and giving the balance of time. This is cool, the possibilities…
This looks great! How hard would it be to adjust it so that it displays:
hours:minutes:seconds
// duplicate minutes and call it hours. on return add the hours value. done!
function formatTime(time) {
const hours = Math.floor(time / 60);
const minutes = Math.floor(time / 60);
let seconds = time % 60;
if (seconds < 10) {
seconds =
0${seconds}
;}
return
${hours}:${minutes}:${seconds}
;}
The method name in step 2 doesn’t match the method name in the template in step 2.
formatTimeLeft / formatTime
Great tutorial!
Hi love this timer
I need multiple timers on one page, and each one to start on a click action.
When I tried, I get JS error “FULL_DASH_ARRAY” has already been declared, plus the other const VARS I added _1, _2, _3 etc to each VAR in each timer instance “FULL_DASH_ARRAY_1” but got the same issue.
any ideas?
On Ie8 , some how this animation is not working. Please suggest
Great Thing. Love it.
What do I have to do remove the minutes and the : when under 1 minute is left?
Also, how could I make it so it shows Some Text when Countdown reaches 00 ?
Awesome tutorial! I’m using it in my project. However, there is a little bug that I can’t solve. The circle starts moving only after one or two seconds. When it reaches 10 seconds the circle should be in the middle.
I figured that when calculating the dashArray for the first time the function calculateTimeFraction is getting 19, 20 as parameters. I thought however that it should be using ‘20,20’ since the timer has just started.
const circleDashArray =
${(
;calculateTimeFraction(timeLeft, timeLimit) * FULL_DASH_ARRAY
).toFixed(0)} 283
How can I have this behavior using react hooks?
I hope this helps someone. As you may notice, the animation starts moving after 1 second (in the last example starts at 19 sec). The reason is because of the interval of 1 sec. The application waits 1 second, and then starts moving.
To solve this, start the animation before setting the interval. Example:
Additionally, you don’t need the fix at calculating the time fraction. Leaving it as timeLeft / TIME_LIMIT works perfectly.
Thanks, I really liked this article and learn from it a lot!
This is beautiful and perfect for what I want to do for a gym timer.
Any thoughts on a direction on how to incorporate a “remote control” for this using a phone?
I’m a new(ish) at programming, but my first thought would be something like a web socket to a server to check if the timer is reset, stopped, etc. Is this a direction I should consider for this task. Just looking to be pointed on a direction for research.
Hi,
Thanks for the step-by-step tutorial! I wonder, why “r” in 2pir formula equals 45?
The circle width and height is 300px, so I guess the radius should be 150 (half of diameter)?
Please, explain anybody. Thanks!
hi for some reason it just seems not to be working.. firstly the FULL_DASH_ARRAY isnt defined and the animation isnt working too
After the countdown, is it possible to add download button to it that will be reveal after it reached zero
The tutorial misses the definition of FULL_DASH_ARRAY so the green progress never moves.
Based on the stroke-dasharray setting, I created the variable (placed on the line under the other const variable TIME_LIMIT).
If you are having this issue you need to add the following into your js:
const FULL_DASH_ARRAY = 283;
Hi,
Thank you for the script.
After reach ZERO, how can i reset it when i call the startTimer() function again? Thanks
Regards, Nuno