Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple countdown timer in Javascript
/********************************************************************************************************************
Countdown.js is a simple script to add a countdown timer
for your website. Currently it can only do full minutes
and partial minutes aren't supported. This script is a fork of http://jsfiddle.net/HRrYG/ with some
added extensions. Since the original code that I forked was released under Creative Commons by SA license,
I have to release this code under the same license. You can view a live demo of this code at http://jsfiddle.net/JmrQE/2/.
********************************************************************************************************************/
function countdown(minutes) {
var seconds = 60;
var mins = minutes
function tick() {
//This script expects an element with an ID = "counter". You can change that to what ever you want.
var counter = document.getElementById("counter");
var current_minutes = mins-1
seconds--;
counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if( seconds > 0 ) {
setTimeout(tick, 1000);
} else {
if(mins > 1){
countdown(mins-1);
}
}
}
tick();
}
//You can use this script with a call to onclick, onblur or any other attribute you would like to use.
countdown(n);//where n is the number of minutes required.
@kevsiraki1

This comment has been minimized.

Copy link

@kevsiraki1 kevsiraki1 commented Jul 4, 2016

Very useful and helpful code... Thank you very much for posting this code!

@abrahamfast

This comment has been minimized.

Copy link

@abrahamfast abrahamfast commented Sep 5, 2016

nice :)

@think2011

This comment has been minimized.

Copy link

@think2011 think2011 commented Oct 10, 2016

nice :) +1

@RichardAgama

This comment has been minimized.

Copy link

@RichardAgama RichardAgama commented Dec 4, 2017

💯

@emersonlopxs

This comment has been minimized.

Copy link

@emersonlopxs emersonlopxs commented Jan 22, 2018

hey I want to stop the timer without reloading the page, ex when the user hits a button, how can I do that?

@obfusticatedcode

This comment has been minimized.

Copy link

@obfusticatedcode obfusticatedcode commented Sep 3, 2018

Brilliant thanks

@Habenzy

This comment has been minimized.

Copy link

@Habenzy Habenzy commented Oct 17, 2018

This is amazing! Bravo sir.

@sajidkhaki

This comment has been minimized.

Copy link

@sajidkhaki sajidkhaki commented Jan 4, 2019

I want to reset the timer on some event how can I do that?

@OldManBrook

This comment has been minimized.

Copy link

@OldManBrook OldManBrook commented Feb 9, 2019

3 minute countdown jumps from 01 to 59 seconds on each iteration.

Amended line 20 as below to count the 00 seconds with a 1000ms timeout

setTimeout(function(){
if(mins > 1){
countdown(mins-1);
}
},1000);

@OldManBrook

This comment has been minimized.

Copy link

@OldManBrook OldManBrook commented Feb 9, 2019

I want to reset the timer on some event how can I do that?

countdown(n);//where n is the number of minutes required.

@bearcub3

This comment has been minimized.

Copy link

@bearcub3 bearcub3 commented May 12, 2019

Your code wasn't verbose. It was easy to understand. Thanks for your work!
It was a great help for my project.
I actually forked your snippet in my gist, modified the coding a little bit for my app and want to share with others.
my version countdown

@bearcub3

This comment has been minimized.

Copy link

@bearcub3 bearcub3 commented May 12, 2019

I want to reset the timer on some event how can I do that?

https://jsfiddle.net/gounshin/as6drbyL/1/

@fkhan698

This comment has been minimized.

Copy link

@fkhan698 fkhan698 commented May 27, 2019

I am trying to add a play button and pause button to this timer. I got my pause button to work, but my I'm not sure how to get my play button to continue. Here is my Codepen

@janimuhlestein

This comment has been minimized.

Copy link

@janimuhlestein janimuhlestein commented Aug 18, 2020

Just perfect. Thank you!

@thinkfink

This comment has been minimized.

Copy link

@thinkfink thinkfink commented Dec 22, 2020

Works great, thank you!

@PopsJones64

This comment has been minimized.

Copy link

@PopsJones64 PopsJones64 commented Jun 13, 2021

Perfect, thank you!

@PrismaticDevelopmentStudios

This comment has been minimized.

Copy link

@PrismaticDevelopmentStudios PrismaticDevelopmentStudios commented Nov 9, 2021

Hey thank you, I just used this in a quiz application for my web dev class
https://prismaticdevelopmentstudios.github.io/web-apis_code-quiz/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment