Skip to content

Instantly share code, notes, and snippets.

@hsharghi
Forked from adhithyan15/countdown.js
Created January 4, 2017 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsharghi/bebd0a0aedf2d8b34677cf6d4d332610 to your computer and use it in GitHub Desktop.
Save hsharghi/bebd0a0aedf2d8b34677cf6d4d332610 to your computer and use it in GitHub Desktop.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment