countdown.js
9.33 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* countdown.js.
* @author hgwang
* @date 2016-05-29
*/
'use strict';
let $ = require('yoho-jquery');
let tip = require('js/plugin/tip');
let EVENT_AFTER_PAINT = 'afterPaint';
let defaultOPtions = {
el: {},
// unix时间戳,单位应该是毫秒!
stopPoint: 0,
leftTime: 0,
template: '', // '${h}时${m}分${s-ext}秒'
varRegular: /\$\{([\-\w]+)\}/g,
clock: ['d', 100, 2, 'h', 24, 2, 'm', 60, 2, 's', 60, 2, 'u', 10, 1],
effect: 'normal'
};
let effect = {
normal: {
paint: function() {
let me = this,
content;
// 找到值发生改变的hand
$.each(me.hands, function(index, hand) {
if (hand.lastValue !== hand.value) {
// 生成新的markup
content = '';
$.each(me._toDigitals(hand.value, hand.bits), function(i, digital) {
content += me._html(digital, '', 'digital');
});
// 并更新
hand.node.html(content);
}
});
}
}
};
let timer = (function() {
let fns = [],
commands = [];// 操作指令
/**
* timer
* 调用频率为100ms一次。努力精确计时,调用帧函数
*/
function timerIn() {
// 计算新时间,调整diff
let diff = +new Date() - timerIn.nextTime,
count = 1 + Math.floor(diff / 100);
// 循环处理fns二元组
let frequency, step,
i, len;
// 为避免循环时受到 对fns数组操作 的影响,
// add/remove指令提前统一处理
while (commands.length) {
commands.shift()();
}
diff = 100 - diff % 100;
timerIn.nextTime += 100 * count;
for (i = 0, len = fns.length; i < len; i += 2) {
frequency = fns[i + 1];
// 100次/s的
if (frequency === 0) {
fns[i](count);
// 1000次/s的
} else {
// 先把末位至0,再每次加2
frequency += 2 * count - 1;
step = Math.floor(frequency / 20);
if (step > 0) {
fns[i](step);
}
// 把末位还原成1
fns[i + 1] = frequency % 20 + 1;
}
}
// next
setTimeout(timerIn, diff);
}
// 首次调用
timerIn.nextTime = +new Date();
timerIn();
function indexOf(item, arr) {
let i, len;
for (i = 0, len = arr.length; i < len; ++i) {
if (arr[i] === item) {
return i;
}
}
return -1;
}
return {
add: function(fn, frequency) {
commands.push(function() {
fns.push(fn);
fns.push(frequency === 1000 ? 1 : 0);
});
},
remove: function(fn) {
let i;
commands.push(function() {
i = indexOf(fn, fns);
if (i !== -1) {
fns.splice(indexOf(fn, fns), 2);
}
});
}
};
}());
function Countdown(config) {
let cfg;
if (!(this instanceof Countdown)) {
return new Countdown(config);
}
config.el = $(config.el);
if (!config.el) {
return;
}
cfg = config.el.attr('data-config');
if (cfg) {
cfg = JSON.parse(cfg.replace(/'/g, '"'));
config = $.extend(true, {}, defaultOPtions, cfg, config);
}
this.config = config;
this._init();
}
$.extend(Countdown.prototype, {
/**
* 初始化
* @private
*/
_init: function() {
let me = this;
let el = me.config.el;
// 初始化时钟.
let hands = [];
// 分析markup
let tmpl = el.html();
let varRE = me.config.varRegular;
let clock;
let _reflow;
/**
* 指针结构
* hand: {
* type: string,
* value: number,
* lastValue: number,
* base: number,
* radix: number,
* bits: number,
* node: S.Node
* }
*/
me.hands = hands;
me.frequency = 1000;
me._notify = [];
varRE.lastIndex = 0;
el.html(tmpl.replace(varRE, function(str, type) {
// 生成hand的markup
let content = '';
// 时钟频率校正.
if (type === 'u' || type === 's-ext') {
me.frequency = 100;
}
if (type === 's-ext') {
hands.push({type: 's'});
hands.push({type: 'u'});
content = me._html('', 's', 'handlet') +
me._html('.', '', 'digital') +
me._html('', 'u', 'handlet');
} else {
hands.push({type: type});
}
return me._html(content, type, 'hand');
}));
// 指针type以外属性(node, radix, etc.)的初始化.
clock = me.config.clock;
$.each(hands, function(index, hand) {
let type = hand.type,
base = 100, i;
hand.node = el.find('.hand-' + type);
// radix, bits 初始化.
for (i = clock.length - 3; i > -1; i -= 3) {
if (type === clock[i]) {
break;
}
base *= clock[i + 1];
}
hand.base = base;
hand.radix = clock[i + 1];
hand.bits = clock[i + 2];
});
me._getLeft();
me._reflow();
// bind reflow to me.
_reflow = me._reflow;
me._reflow = function() {
return _reflow.apply(me, arguments);
};
timer.add(me._reflow, me.frequency);
// 显示时钟.
el.show();
},
/**
* 获取倒计时剩余帧数
*/
_getLeft: function() { // {{{
let left = this.config.leftTime * 1000;
let end = this.config.stopPoint; // 这个是UNIX时间戳,毫秒级
if (!left && end) {
left = end - (+new Date());
}
this.left = left - left % this.frequency;
}, // }}}
/**
* 更新时钟
*/
_reflow: function(count) {
let me = this;
let el = me.config.el;
count = count || 0;
me.left = me.left - me.frequency * count;
// 更新hands
$.each(me.hands, function(index, hand) {
hand.lastValue = hand.value;
hand.value = Math.floor(me.left / hand.base) % hand.radix;
});
// 更新时钟.
me._repaint();
// notify
if (me._notify[me.left]) {
$.each(me._notify[me.left], function(index, callback) {
callback.call(me);
});
}
// notify 可能更新me.left
if (me.left < 1) {
el.text('— 活动已结束 —');
el.parents('.back-ground-white').on('click', function() {
tip.show('活动即将开始,敬请期待!');
return false;
});
timer.remove(me._reflow);
}
if (me.left < 86400000) {
el.find('.left-day').hide();
}
return me;
},
/**
* 重绘时钟
* @private
*/
_repaint: function() {
effect[this.config.effect].paint.apply(this);
this.config.el.trigger(EVENT_AFTER_PAINT);
},
/**
* 把值转换为独立的数字形式
* @private
* @param {number} value
* @param {number} bits
*/
_toDigitals: function(value, bits) {
let digitals = [];
value = value < 0 ? 0 : value;
value.toString().length > 1 ? bits = 2 : bits = 1;
// 把时、分、秒等换算成数字.
while (bits--) {
digitals[bits] = value % 10;
value = Math.floor(value / 10);
}
return digitals;
},
/**
* 生成需要的html代码,辅助工具
* @private
* @param {string|Array.<string>} content
* @param {string} className
* @param {string} type
*/
_html: function(content, className, type) {
if ($.isArray(content)) {
content = content.join('');
}
switch (type) {
case 'hand':
className = type + ' hand-' + className;
break;
case 'handlet':
className = type + ' hand-' + className;
break;
case 'digital':
if (content === '.') {
className = type + ' ' + type + '-point ' + className;
} else {
className = type + ' ' + type + '-' + content + ' ' + className;
}
break;
default:
break;
}
return '<i class="' + className + '">' + content + '</i>';
},
/**
* 倒计时事件
* @param {number} time unit: second
* @param {Function} callback
*/
notify: function(time, callback) {
let notifies;
time = time * 1000;
time = time - time % this.frequency;
notifies = this._notify[time] || [];
notifies.push(callback);
this._notify[time] = notifies;
return this;
}
});
exports.Countdown = Countdown;