-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.countdown.min.js
91 lines (82 loc) · 3.76 KB
/
jquery.countdown.min.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* @Author: Alex Dong
* @Date: 2020-07-29 13:21:07
* @Last Modified by: Alex Dong
* @Last Modified time: 2022-07-10 10:48:03
*/
(function($) {
"use strict";
$.fn.countdown = function(options) {
var defaults = {
until: '',
layout: '<span class="box-count day"><span class="number">0</span><span class="text">Days</span></span><span class="box-count hrs"><span class="number">0</span><span class="text">Hrs</span></span><span class="box-count min"><span class="number">0</span><span class="text">Mins</span></span><span class="box-count secs"><span class="number">0</span> <span class="text">Secs</span></span>',
leadingZero: true,
countStepper: -1, // s: -1 // min: -60 // hour: -3600
timeout: '<span class="timeout">Time out!</span>',
};
var settings = $.extend(defaults, options),
until = settings.until,
layout = settings.layout,
leadingZero = settings.leadingZero,
countStepper = settings.countStepper,
setTimeOutPeriod = (Math.abs(countStepper) - 1) * 1000 + 990,
timeout = settings.timeout;
var methods = {
init: function() {
return this.each(function() {
var $countdown = $(this);
if ($countdown.length && !$countdown.hasClass('init')) {
$countdown.addClass('init');
methods.timerLoad($countdown);
}
});
},
timerLoad: function(el) {
var gsecs = (new Date(until).getTime() > 0) ? until : el.data('timer');
if (typeof gsecs === 'string') gsecs = gsecs.replace(/-/g, '/');
if (isNaN(gsecs) || typeof gsecs === 'object') {
var start = Date.parse(new Date());
var end = isNaN(gsecs) ? Date.parse(gsecs) : gsecs;
var end = (typeof gsecs === 'object') ? gsecs : Date.parse(gsecs);
gsecs = (end - start) / 1000;
}
if (gsecs > 0) {
var isLayout = el.find('.min .number');
if (!isLayout.length) {
el.html(layout);
}
methods.CountBack(el, gsecs);
} else {
el.html(timeout);
}
},
calcage: function(secs, num1, num2) {
var s = ((Math.floor(secs / num1) % num2)).toString();
if (leadingZero && s.length < 2) s = "0" + s;
return "<b>" + s + "</b>";
},
CountBack: function(el, secs) {
var count = setInterval(function timer() {
if (secs < 0) {
clearInterval(count);
el.html(timeout);
return;
}
el.find('.day .number').html(methods.calcage(secs, 86400, 100000));
el.find('.hour .number, .hrs .number').html(methods.calcage(secs, 3600, 24));
el.find('.min .number').html(methods.calcage(secs, 60, 60));
el.find('.sec .number, .secs .number').html(methods.calcage(secs, 1, 60));
secs += countStepper;
return timer;
}(), setTimeOutPeriod);
}
};
if (methods[options]) {
return methods[options].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof options === 'object' || !options) {
return methods.init.apply(this);
} else {
$.error('Method "' + method + '" does not exist in timer plugin!');
}
};
})(jQuery);