-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.min.js
364 lines (297 loc) · 10 KB
/
index.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
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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.$timers = {}));
})(this, (function (exports) { 'use strict';
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", {
writable: false
});
return Constructor;
}
/**
* Class: Timer
*/
var Timer = /*#__PURE__*/function () {
function Timer() {
this._interval = 1000;
this._initInterval = 1000;
this._tick;
this._isTimerStop = false; //Whether is the immediately stop
this.timer = null;
this._startTime = null;
this._tickCount = 0;
this.missTickEnabled = false; //若延迟误差超过了间隔时间,则忽略miss掉中间,还是追加补偿tick
this._delta = 0; //由于补偿后,在执行setTimeout时,仍会消耗1-2 ms导致1-2 ms误差,若还需要差量补偿,可以更改这个参数
} ///时间间隔(单位毫秒ms),默认1s
var _proto = Timer.prototype;
// --------- public 方法------------
_proto.start = function start() {
this._isTimerStop = false;
this._startTime = new Date().getTime();
clearTimeout(this.timer);
this.startSetTimeout();
};
_proto.stop = function stop() {
this._isTimerStop = true;
this._tickCount = 0;
clearTimeout(this.timer);
} ///获取定时器是否停止状态
;
//----------------- private 方法---------------------
_proto.isFunction = function isFunction(func) {
return func && Object.prototype.toString.call(func) === '[object Function]';
};
_proto.startSetTimeout = function startSetTimeout() {
var _this = this;
this.timer = setTimeout(function () {
_this.tickEvent();
}, this._interval - this._delta);
};
_proto.tickEvent = function tickEvent() {
if (!this.isFunction(this._tick)) {
this.stop();
throw new Error("tick:" + this._tick + ",tick is not function.");
}
if (!this._isTimerStop) {
this._tickCount++;
this.offsetDiff();
this.startSetTimeout();
this._tick();
}
};
_proto.offsetDiff = function offsetDiff() {
var offset = new Date().getTime() - (this._startTime + this._tickCount * this._initInterval); // console.log("误差:" + offset) //由于补偿后,在执行setTimeout时,仍会消耗1-2 ms导致1-2 ms误差,
var nextTime = this._initInterval - offset;
if (nextTime < 0 && this._missTickEnabled) {
//若小于0,表示误差大于间隔,已经错过了一次tick,若错过tick,则应该排除多余的tick做补偿
this._interval = this._initInterval - Math.abs(nextTime) % this._initInterval;
} else {
this._interval = nextTime >= 0 ? nextTime : 0;
}
};
_createClass(Timer, [{
key: "interval",
get: function get() {
return this._initInterval;
},
set: function set(seconds) {
this._interval = seconds;
this._initInterval = seconds;
} //差量,因为setTimeout的js执行也需要时间,所以可以设置差量,达到最小误差.
}, {
key: "delta",
get: function get() {
return this._delta;
},
set: function set(val) {
this._delta = val;
} ///时间定时器定时执行函数
}, {
key: "tick",
get: function get() {
return this._tick();
},
set: function set(func) {
if (!this.isFunction(func)) {
throw new Error("tick is not function.");
}
this._tick = func;
}
}, {
key: "missTickEnabled",
get: function get() {
return this._missTickEnabled;
},
set: function set(val) {
this._missTickEnabled = !!val;
}
}, {
key: "isTimerStop",
get: function get() {
return this._isTimerStop;
}
}]);
return Timer;
}();
/**
* Enum: Status
*/
var CountDownTimerStatus = Object.freeze({
//运行
running: 'running',
//停止
stop: 'stop',
//暂停
pause: 'pause'
});
/**
* Enum: Mode
*/
// const CountDownTimeoutMode = Object.freeze({
// loop: Symbol(0),
// reset: Symbol(1)
// })
var CountDownTimeoutMode = Object.freeze({
//Stop when the countdown is over
once: 'once',
//after the timer stop,restart
loop: 'loop',
//reset countdown after user operation
reset: 'reset'
});
/*倒计时类
*/
var CountDownTimer = /*#__PURE__*/function () {
function CountDownTimer() {
var _this = this;
this._remainingSeconds = 60;
this._timeoutSeconds = 60;
this._timerStatus = CountDownTimerStatus.pause; //操作定时状态
this._timeoutMode = CountDownTimeoutMode.once; // 操作超时模式
this.onTimeout = null; //超时触发器
this.tick = null; //倒计时触发器
this.mainTimer = new Timer();
this.mainTimer.interval = 1000; //主要定时器时间间隔
this.mainTimer.tick = function () {
_this.mainTimerEvent();
};
} //超时剩余时间
var _proto = CountDownTimer.prototype;
/*--------设置操作时间超时------[公有方法]----------------*/
_proto.setTimeout = function setTimeout(seconds) {
this.stop();
this._timeoutSeconds = seconds;
this._remainingSeconds = seconds;
} //启动操作
;
_proto.start = function start() {
// console.log(`CountDownTimer start: ${this.timeoutSeconds}s`);
if (this._timeoutMode === CountDownTimeoutMode.reset) {
this.listenActiveEvent();
}
this._remainingSeconds = this._timeoutSeconds;
this._timerStatus = CountDownTimerStatus.running;
this.mainTimer.start();
} //监听用户操作激活
;
_proto.listenActiveEvent = function listenActiveEvent() {
var _this2 = this;
var body = document.querySelector('html');
var activeFun = function activeFun(event) {
body.removeEventListener("click", activeFun);
body.removeEventListener("keydown", activeFun);
body.removeEventListener("mousemove", activeFun);
body.removeEventListener("mousewheel", activeFun);
_this2.reset();
};
body.addEventListener("click", activeFun);
body.addEventListener("keydown", activeFun);
body.addEventListener("mousemove", activeFun);
body.addEventListener("mousewheel", activeFun);
} //停止操作
;
_proto.stop = function stop() {
this._remainingSeconds = 0;
this._timerStatus = CountDownTimerStatus.stop;
this.mainTimer.stop();
} //暂停操作
;
_proto.pause = function pause() {
if (this._timerStatus == CountDownTimerStatus.running) {
this._timerStatus = CountDownTimerStatus.pause;
this.mainTimer.stop();
}
} //恢复倒计时
;
_proto.resume = function resume() {
if (this._timerStatus != CountDownTimerStatus.pause) {
return;
}
this.mainTimer.start();
this._timerStatus = CountDownTimerStatus.running;
} //重置倒计时
;
_proto.reset = function reset() {
if (this._timerStatus === CountDownTimerStatus.stop) {
return;
}
this.stop();
this.start();
}
/*-----------------------------------------------*/
/*----倒计时定时器触发事件------[私有方法]----------*/
;
_proto.isFunction = function isFunction(func) {
return func && Object.prototype.toString.call(func) === '[object Function]';
};
_proto.mainTimerEvent = function mainTimerEvent() {
this._remainingSeconds--;
if (this.isFunction(this.tick)) {
if (this._remainingSeconds <= 0 && this._timeoutMode === CountDownTimeoutMode.loop) {
this._remainingSeconds = this._timeoutSeconds;
} //tick事件需在ontimeout之前触发
this.tick(this._remainingSeconds);
}
if (this._remainingSeconds <= 0) {
if (this._timeoutMode === CountDownTimeoutMode.once) {
this._timerStatus = CountDownTimerStatus.stop;
this.stop();
} else if (this._timeoutMode === CountDownTimeoutMode.loop) {
this.reset();
} //loop模式,每次也应该触发超时事件
if (this.isFunction(this.onTimeout)) {
this.onTimeout();
}
}
};
_createClass(CountDownTimer, [{
key: "remainingSeconds",
get: function get() {
return this._remainingSeconds;
} // set remainingSeconds(seconds) {
// this._remainingSeconds = seconds;
// }
//超时时间
}, {
key: "timeoutSeconds",
get: function get() {
return this._timeoutSeconds;
},
set: function set(seconds) {
this._timeoutSeconds = seconds;
this._remainingSeconds = seconds;
} //定时器状态
}, {
key: "timerStatus",
get: function get() {
return this._timerStatus;
} //超时模式
}, {
key: "timeoutMode",
get: function get() {
return this._timeoutMode;
},
set: function set(mode) {
this._timeoutMode = mode;
}
}]);
return CountDownTimer;
}();
exports.CountDownTimeoutMode = CountDownTimeoutMode;
exports.CountDownTimer = CountDownTimer;
exports.CountDownTimerStatus = CountDownTimerStatus;
exports.Timer = Timer;
Object.defineProperty(exports, '__esModule', { value: true });
}));