Countdown.js is a little yet customizable countdown made in pure JavaScript.
// Instanciating a new countdown with all defaults
new Countdown();
// Instanciating a custom countdown
var countdown = new Countdown({
selector: '#timer',
msgBefore: "Will start at Christmas!",
msgAfter: "Happy new year folks!",
msgPattern: "{days} days, {hours} hours and {minutes} minutes before new year!",
dateStart: new Date('2013/12/25 12:00'),
dateEnd: new Date('Jan 1, 2014 12:00')
});
You can also play around with the code at CodePen.
You can pass the constructor number of options, including:
The selector you want to inject Countdown into. It should be a valid string for document.querySelector()
.
Default: .timer
The message to display before reaching dateStart
.
Default: "Be ready!"
The message to display once reaching dateEnd
.
Default: "It's over, sorry folks!"
The message to display during the countdown where values between braces get replaced by actual numeric values. Possible patterns:
{years}
{months}
{weeks}
{days}
{hours}
{minutes}
{seconds}
Default: "{days} days, {hours} hours, {minutes} minutes and {seconds} seconds left."
The date to start the countdown to. Should be a valid instance of class Date
. Documentation here at MDN.
Default: new Date()
(now)
The date to end the countdown to. Should be a valid instance of class Date
. Documentation here at MDN.
Default: new Date(new Date().getTime() + (24 * 60 * 60 * 1000))
(tomorrow)
The function to be called whenever the countdown starts.
Default: null
The function to be called whenever the countdown stops.
Default: null
Defines whether or not leading zeros are displayed when numbers are between 0
and 9
.
Default: null
Defines whether or not the countdown should be initialized when instancied. If set to false
, you can manually launch it with .initialize()
.
Default: true
The script doesn't require jQuery at all meanwhile it fires two events on your element if you happen to have jQuery loaded: countdownStart
and countdownEnd
. You can use them this way:
new Countdown({
selector: '.timer'
})
$('.timer').on('countdownStart', function() {
// do something
});
grunt test (running JShint and Jasmine)
grunt deploy (running UglifyJS)