forked from iwayanwirka/dscountdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dscountdown.js
204 lines (167 loc) · 6.12 KB
/
dscountdown.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*!
dsCountDown v1.1
jQuery count down plugin
(c) 2013 I Wayan Wirka - http://iwayanwirka.duststone.com/dscountdown/
license: http://www.opensource.org/licenses/mit-license.php
*/
(function($){
$.fn.dsCountDown = function(givenOptions){
var ds = this;
ds.data = {
refreshed : 1000,
thread : null,
running : false,
left : 0,
decreament : 1,
interval : 0,
seconds : 0,
minutes : 0,
hours : 0,
days : 0,
elemDays: null,
elemHours: null,
elemMinutes: null,
elemSeconds: null
};
var defaults = {
startDate: new Date(), // Date Object of starting time of count down, usualy now (whether client time or given php time)
endDate: null, // Date Object of ends of count down
elemSelDays: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.days
elemSelHours: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.hours
elemSelMinutes: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.minutes
elemSelSeconds: '', // Leave blank to use default value or provide a string selector if the lement already exist, Example: .ds-ds.data.seconds
theme: 'white', // Set the theme 'white', 'black', 'red', 'flat', 'custome'
titleDays: 'Days', // Set the title of ds.data.days
titleHours: 'Hours', // Set the title of ds.data.hours
titleMinutes: 'Minutes', // Set the title of ds.data.minutes
titleSeconds: 'Seconds', // Set the title of ds.data.seconds
onBevoreStart: null, // callback before the count down starts
onClocking: null, // callback after the timer just clocked
onFinish: null // callback if the count down is finish or 0 timer defined
};
ds.options = $.extend( {}, defaults, givenOptions );
if (this.length > 1){
this.each(function() { $(this).dsCountDown(givenOptions) });
return this;
}
ds.init = function(){
// Init element
if(! ds.options.elemSelSeconds){
ds.prepend('<div class="ds-element ds-element-seconds">\
<div class="ds-element-title">'+ ds.options.titleSeconds +'</div>\
<div class="ds-element-value ds-seconds">00</div>\
</div>');
ds.data.elemSeconds = ds.find('.ds-seconds');
}else{
ds.data.elemSeconds = ds.find(ds.options.elemSelSeconds);
}
if(! ds.options.elemSelMinutes){
ds.prepend('<div class="ds-element ds-element-minutes">\
<div class="ds-element-title">'+ ds.options.titleMinutes +'</div>\
<div class="ds-element-value ds-minutes">00</div>\
</div>');
ds.data.elemMinutes = ds.find('.ds-minutes');
}else{
ds.data.elemMinutes = ds.find(ds.options.elemSelMinutes);
}
if(! ds.options.elemSelHours){
ds.prepend('<div class="ds-element ds-element-hours">\
<div class="ds-element-title">'+ ds.options.titleHours +'</div>\
<div class="ds-element-value ds-hours">00</div>\
</div>');
ds.data.elemHours = ds.find('.ds-hours');
}else{
ds.data.elemHours = ds.find(ds.options.elemSelHours);
}
if(! ds.options.elemSelDays){
ds.prepend('<div class="ds-element ds-element-days">\
<div class="ds-element-title">'+ ds.options.titleDays +'</div>\
<div class="ds-element-value ds-days">00</div>\
</div>');
ds.data.elemDays = ds.find('.ds-days');
}else{
ds.data.elemDays = ds.find(ds.options.elemSelDays);
}
ds.addClass('dsCountDown');
ds.addClass('ds-' + ds.options.theme);
// Init start and end
if(ds.options.startDate && ds.options.endDate){
ds.data.interval = ds.options.endDate.getTime() - ds.options.startDate.getTime();
if(ds.data.interval > 0){
var allSeconds = (ds.data.interval / 1000);
var hoursMod = (allSeconds % 86400);
var minutesMod = (hoursMod % 3600);
ds.data.left = allSeconds;
ds.data.days = Math.floor(allSeconds / 86400);
ds.data.hours = Math.floor(hoursMod / 3600);
ds.data.minutes = Math.floor(minutesMod / 60);
ds.data.seconds = Math.floor(minutesMod % 60);
}
}
ds.start();
}
ds.stop = function(callback){
if(ds.data.running){
clearInterval(ds.data.thread);
ds.data.running = false;
}
if(callback){
callback(ds);
}
}
ds.start = function(){
$('#logger').append('<br/>Start');
if(! ds.data.running){
$('#logger').append('<br/>Clock');
if(ds.data.left > 0){
if(ds.options.onBevoreStart){
ds.options.onBevoreStart(ds);
}
ds.data.thread = setInterval(
function(){
if(ds.data.left > 0){
ds.data.left -= ds.data.decreament;
ds.data.seconds -= ds.data.decreament;
if(ds.data.seconds < 0 && (ds.data.minutes > 0 || ds.data.hours > 0 || ds.data.days > 0)){
ds.data.minutes --;
ds.data.seconds = 59;
}
if(ds.data.minutes < 0 && (ds.data.hours > 0 || ds.data.days > 0)){
ds.data.hours --;
ds.data.minutes = 59;
}
if(ds.data.hours < 0 && ds.data.days > 0){
ds.data.days --;
ds.data.hours = 23;
}
if(ds.data.elemDays)
ds.data.elemDays.html((ds.data.days < 10 ? '0' + ds.data.days : ds.data.days));
if(ds.data.elemHours)
ds.data.elemHours.html((ds.data.hours < 10 ? '0' + ds.data.hours : ds.data.hours));
if(ds.data.elemMinutes)
ds.data.elemMinutes.html((ds.data.minutes < 10 ? '0' + ds.data.minutes : ds.data.minutes));
if(ds.data.elemSeconds)
ds.data.elemSeconds.html((ds.data.seconds < 10 ? '0' + ds.data.seconds : ds.data.seconds));
if(ds.options.onClocking){
ds.options.onClocking(ds);
}
}else{
ds.stop(ds.options.onFinish);
}
},
ds.data.refreshed);
ds.data.running = true;
}else{
if(ds.options.onFinish){
ds.options.onFinish(ds);
}
}
}
}
ds.init();
// var restart = function(){
// stop();
// start();
// }
}
})(jQuery);