-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery.scountdown.js
58 lines (46 loc) · 1.35 KB
/
jquery.scountdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*!
* Simple jQuery Countdown
* https://github.com/jonasanx/jQuery-simple-countdown
* @author Jonathan J. Flores
*/
;(function ($) {
$.scountdown = function (element, options) {
var plugin = this;
var time = {
days: 24 * 60 * 60,
hours: 60 * 60,
minutes: 60
};
plugin.init = function () {
var settings = $.extend({}, {
timestamp: 0,
callback: function () {}
}, options);
(function countdown() {
var timeLeft = Math.floor((settings.timestamp - (new Date())) / 1000);
var digits = {}
if (timeLeft < 0) timeLeft = 0;
for (key in time) {
var y = time[key];
var x = digits[key] = add_leading_zero(Math.floor(timeLeft / y));
timeLeft -= x * y;
}
// Returns days, hours, minutes, and seconds.
// TODO: arguments should be passed dynamically.
settings.callback(digits.days, digits.hours, digits.minutes, add_leading_zero(timeLeft));
setTimeout(countdown, 1000);
})();
}
// Private methods
var add_leading_zero = function (n) {
return (n.toString().length < 2) ? '0' + n : n;
}
// Fire!
plugin.init();
}
$.fn.scountdown = function (options) {
return this.each(function () {
var plugin = new $.scountdown(this, options);
});
}
})(jQuery);