-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
275 lines (243 loc) · 7.41 KB
/
index.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
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
/*!
* count-time-down@1.0.5
* A helpful countdown class, 一个实用的的倒计时类
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.CountDown = factory());
})(this, (function () { 'use strict';
var isFun = function isFun(value) {
return typeof value === 'function';
};
var isObj = function isObj(value) {
return Object.prototype.toString.call(value) === '[object Object]';
};
var isNum = function isNum(value) {
return typeof value === 'number' && !Number.isNaN(value) && Number.isFinite(value);
};
/**
* 一个倒计时专用工具类,可用来创建倒计时对象
* @param {number} time 倒计时时间
* @param {Object} options 倒计时参数
* @param {Function} tickCallback 倒计时步进回调
*
* @example
* import CountDown from 'count-time-down';
* // In NodeJS
* const CountDown = require('count-time-down');
*
* 1. 创建并自动开启一个24小时的倒计时
* new CountDown(864e5, cd => console.log(cd.hhmmss));
*
* 2. 创建并自动开启一个60s的倒计时
* new CountDown(10000, { cdType: 's' }, cd => console.log(cd.s));
*
* 3. 创建一个可以显示毫秒的定时器
* new CountDown(10000, { interval: 50 }, ({ss, SSS}) => {
* console.log(`${ss} ${SSS}`);
* });
*
* 4. 创建一个60s倒计时,手动开始和结束
* const cd = new CountDown(60000, { autoStart: false }, () => {
* console.log(cd);
* });
* // A moment later
* cd.start();
*
* 5. 创建一个倒计时,自定义参数再启动
* const cd = new CountDown();
* cd.time = 10000;
* cd.cdType = 's';
* cd.onTick = cd => console.log(cd);
* cd.start();
* // A moment later
* cd.stop();
* // A moment later
* cd.start();
* // Destory The countdown
* cd.destory();
*/
function CountDown(time, options, tickCallback) {
var _this3 = this;
if (isFun(time)) {
tickCallback = time;
} else if (isFun(options)) {
tickCallback = options;
}
options = isObj(options) ? options : {};
this.options = options;
// 初始时间
this.initTime = isNum(time) ? time : null;
// 剩余时间
this.restTime = this.time;
// 定时间隔
this.interval = isNum(options.interval) ? options.interval : 1000;
// 是否自动启动倒计时
this.autoStart = options.autoStart !== false;
// 倒计时类型,d: 到天,h: 到小时,m: 到分钟,s: 到秒,S: 到毫秒,默认:'h'.
this.cdType = ['d', 'h', 'm', 's', 'S'].indexOf(options.cdType) > -1 ? options.cdType : 'h';
this.running = false;
this.destoryed = false;
this.completed = false;
this.tickTimes = 0;
this.restDays = null;
this.restHours = null;
this.restMinuts = null;
this.restSeconds = null;
this.restMillisecond = null;
this.d = null;
this.h = null;
this.m = null;
this.s = null;
this.S = null;
this.dd = '--';
this.hh = '--';
this.mm = '--';
this.ss = '--';
this.SSS = '---';
this.ms = '-:-';
this.hms = '-:-:-';
this.mmss = '--:--';
this.hhmmss = '--:--:--';
this.timerId = null;
/**
* 开始倒计时
*/
this.start = function () {
var _this = this;
if (this.destoryed) return;
this.running = true;
if (this.interval >= 0 && this.restTime >= this.interval) {
this.completed = false;
clearInterval(this.timerId);
this.timerId = setInterval(function () {
return _this.tick();
}, this.interval);
} else {
this.setComplete();
}
};
/**
* 暂停倒计时
*/
this.stop = function () {
clearInterval(this.timerId);
this.running = false;
};
/**
* 销毁倒计时
*/
this.destory = function () {
clearInterval(this.timerId);
this.running = false;
this.destoryed = true;
};
/**
* 定时器结束
*/
this.setComplete = function () {
clearInterval(this.timerId);
this.running = false;
this.completed = true;
};
/**
* 定时步进
*/
this.tick = function () {
this.tickTimes++;
if (this.restTime > this.interval) {
this.restTime -= this.interval;
this.setValue();
} else {
this.restTime = 0;
this.setValue();
this.setComplete();
}
if (isFun(this.onTick)) this.onTick(this);
if (isFun(tickCallback)) tickCallback(this);
};
/**
* 设置定时器的值
*/
this.setValue = function () {
var _this2 = this;
if (!this.restTime || this.restTime < 0) this.restTime = 0;
this.restDays = Math.floor(this.restTime / 864e5);
this.restHours = Math.floor(this.restTime / 36e5);
this.restMinuts = Math.floor(this.restTime / 6e4);
this.restSeconds = Math.floor(this.restTime / 1000);
this.restMillisecond = this.restTime;
if (this.cdType === 'd') {
var restSeconds = Math.floor(this.restTime % 864e5 / 1000);
this.d = this.restDays;
this.h = Math.floor(restSeconds / 3600);
this.m = Math.floor(restSeconds % 3600 / 60);
this.s = Math.floor(restSeconds % 60);
} else if (this.cdType === 'h') {
this.d = 0;
this.h = this.restHours;
this.m = Math.floor(this.restSeconds % 3600 / 60);
this.s = Math.floor(this.restSeconds % 60);
} else if (this.cdType === 'm') {
this.d = this.h = 0;
this.m = this.restMinuts;
this.s = Math.floor(this.restSeconds % 60);
} else if (this.cdType === 's') {
this.d = this.h = this.m = 0;
this.s = this.restSeconds;
} else if (this.cdType === 'S') {
this.d = this.h = this.m = this.s = 0;
this.S = this.restMillisecond;
}
if (this.cdType !== 'S') {
this.S = Math.floor(this.restTime % 1000);
}
var dhmsS = 'dhmsS';
dhmsS.substr(dhmsS.indexOf(this.cdType)).split('').forEach(function (item) {
var itemStr = String(_this2[item]);
var itemLen = 2,
itemTpl = item + item;
if (item === 'S') {
itemLen++;
itemTpl += item;
}
_this2[itemTpl] = itemStr.length < itemLen ? ('00' + itemStr).substr(-itemLen) : itemStr;
});
var co = ':';
this.ms = this.m + co + this.s;
this.hms = this.h + co + this.m + co + this.s;
this.mmss = this.mm + co + this.ss;
this.hhmmss = this.hh + co + this.mm + co + this.ss;
['dd', 'hh', 'mm', 'ss', 'ms', 'hms', 'mmss', 'hhmmss'].forEach(function (item) {
_this2[item] = _this2[item].replace(/-/g, '0');
});
};
/**
* 判断是否自动开启
*/
if (isNum(this.time)) {
this.setValue();
if (this.autoStart) {
setTimeout(function () {
return _this3.start();
});
}
}
}
/**
* 初始时间为null时,可以再次设置开始时间
*/
Object.defineProperty(CountDown.prototype, 'time', {
get: function get() {
return this.initTime;
},
set: function set(val) {
if (isNum(val)) {
this.initTime = val;
this.restTime = val;
}
}
});
return CountDown;
}));