A simple jQuery countdown plugin.
Include files:
<script src="/path/to/jquery.js"></script><!-- jQuery is required --> <script src="/path/to/countdown.js"></script>
countdown attributeHTML:
<p countdown>Dec 31 2020 23:59:59</p>
Demo:
Dec 31 2020 23:59:59
data-* attributeHTML:
<p countdown data-fast="true">Dec 31 2020 23:59:59</p>
Demo:
Dec 31 2020 23:59:59
$.fn.countdown methodHTML:
<p class="countdown">Dec 31 2020 23:59:59</p>
Javascript:
$(".countdown").countdown();
Demo:
Dec 31 2020 23:59:59
HTML:
<p id="countdown-add-options"></p>
Javascript:
$("#countdown-add-options").countdown({
date: new Date(2020, 11, 31)
});
Demo:
Setup with $("#target").countdown(options), or global setup with $.fn.countdown.setDefaults(options).
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-fast="true".
| Name | Type | Default | Description |
|---|---|---|---|
| autoStart | boolean | true | Auto start the countdown when initialized. |
| date | object / number / string | null | The target date, allow date object, time number (milliseconds), valid date string. |
| fast | boolean | false | Set it `true` to update the date number per one tenth second. |
| end | function | function() {} | The function will be run when the countdown end. |
| text | string | "%s days, %s hours, %s minutes, %s seconds" | Just a text template, you can customize it, e.g., "%s D / %s H / %s M / %s S". |
HTML:
<p id="countdown-methods">Jan 1 2020 00:00:00</p> <button id="countdown-start">Start</button> <button id="countdown-stop">Stop</button> <button id="countdown-end" class="btn btn-success">End</button> <button id="countdown-destroy" class="btn btn-danger">Destroy</button>
JavaScript:
var $countdownMethods = $("#countdown-methods");
$countdownMethods.countdown({
autoStart: false
});
$("#countdown-start").click(function() {
$countdownMethods.countdown("start");
});
$("#countdown-stop").click(function() {
$countdownMethods.countdown("stop");
});
$("#countdown-end").click(function() {
$countdownMethods.countdown("end");
});
$("#countdown-destroy").click(function() {
$countdownMethods.countdown("destroy");
});
Demo:
Jan 1 2020 00:00:00
Customize content, layout, style by add data-* attribute to the children of countdown container. For example:
HTML:
<div class="countdown" countdown data-date="Jan 1 2017 00:00:00"> <span data-heading>New Year Countdown</span> <span data-days>0</span>days <span data-hours>0</span>hours <span data-minutes>0</span>minutes <span data-seconds>0</span>seconds </div>
Demo:
HTML:
<p countdown data-text="Christmas Day Countdown: %s Days / %s Hours / %s Minutes / %s Seconds">Dec 25 2020 00:00:00</p>
Demo:
Dec 25 2020 00:00:00
HTML:
<p countdown data-date="Dec 25 2016" data-text="圣诞节倒计时:%s 天 %s 时 %s 分 %s 秒"></p>
Demo:
end functionHTML:
<p id="countdown-option-end"></p>
JavaScript:
var $countdownOptionEnd = $("#countdown-option-end");
$countdownOptionEnd.countdown({
date: (new Date()).getTime() + 60 * 1000, // 1 minute later
fast: true,
end: function() {
$countdownOptionEnd.text("Countdown end!");
}
});
Demo: