1
- /**
2
- * 定时器管理库
3
- * @author chenzeji
4
- */
5
-
6
1
class Timer {
7
- /**
8
- * 清除定时器
9
- * @param {String } pageId 页面 id
10
- * @param {String } id 定时器 id
11
- */
12
- static clear ( pageId , id ) {
13
- const { timerStore } = Timer . pageStore . get ( pageId ) || { }
14
- if ( ! timerStore ) return
15
-
16
- const timer = timerStore . get ( id )
17
- if ( ! timer ) return
18
-
19
- clearTimeout ( timer . timerId )
20
- timer . timerId = ''
21
- timerStore . delete ( id )
22
- }
23
-
24
- /**
25
- * 页面加载处理函数
26
- * @param {String } pageId 页面 id
27
- */
28
- static pageLoad ( pageId ) {
29
- Timer . pageStore . set ( pageId , {
30
- isActive : true ,
31
- timerStore : new Map ( )
32
- } )
33
- }
34
-
35
- /**
36
- * 页面展示处理函数
37
- * @param {String } pageId 页面 id
38
- */
39
- static pageShow ( pageId ) {
40
- const page = Timer . pageStore . get ( pageId ) || { }
41
-
42
- /* 没有隐藏,不需要恢复定时器 */
43
- if ( page . isActive ) return
44
-
45
- page . isActive = true
46
- page . timerStore && page . timerStore . forEach ( timer => timer . start ( ) )
47
- }
48
-
49
- /**
50
- * 页面隐藏处理函数
51
- * @param {String } pageId 页面 id
52
- */
53
- static pageHide ( pageId ) {
54
- const page = Timer . pageStore . get ( pageId ) || { }
55
- page . timerStore && page . timerStore . forEach ( timer => timer . suspend ( ) )
56
- page . isActive = false
57
- }
58
-
59
- /**
60
- * 页面卸载处理函数
61
- * @param {String } pageId 页面 id
62
- */
63
- static pageUnLoad ( pageId ) {
64
- Timer . pageHide ( pageId )
65
- Timer . pageStore . delete ( pageId )
66
- }
67
-
68
2
/**
69
3
* 构造函数
70
4
* @param {Boolean } isInterval 是否是 setInterval
71
- * @param {String } pageId 页面 id
72
5
* @param {Function } fn 回调函数
73
6
* @param {Number } timeout 定时器执行时间间隔
74
7
* @param {...any } arg 定时器其他参数
75
8
*/
76
- constructor ( isInterval = false , pageId = '' , fn = ( ) => { } , timeout = 0 , ...arg ) {
9
+ constructor ( isInterval = false , fn = ( ) => { } , timeout = 0 , ...arg ) {
77
10
this . id = ++ Timer . count // 定时器递增 id
78
11
this . fn = fn
79
12
this . timeout = timeout
80
13
this . restTime = timeout // 定时器剩余计时时间
81
- this . pageId = pageId
82
14
this . isInterval = isInterval
83
15
this . arg = arg
84
-
85
- /* 存储定时器 */
86
- const { timerStore } = Timer . pageStore . get ( pageId ) || { }
87
- timerStore && timerStore . set ( this . id , this )
88
-
89
- this . start ( )
90
16
}
91
17
92
- /**
93
- * 启动定时器
94
- */
95
- start ( ) {
96
- const { isActive, timerStore } = Timer . pageStore . get ( this . pageId ) || { }
18
+ /* 启动或恢复定时器 */
19
+ start ( timerStore ) {
97
20
/* 页面隐藏,不创建定时器 */
98
- if ( this . restTime < 0 || ! isActive ) return
99
-
100
21
this . startTime = + new Date ( )
101
22
102
23
if ( this . isInterval ) {
@@ -112,7 +33,7 @@ class Timer {
112
33
/* setTimeout */
113
34
const cb = ( ...arg ) => {
114
35
this . fn ( ...arg )
115
- timerStore && timerStore . delete ( this . id )
36
+ timerStore . delete ( this . id )
116
37
}
117
38
this . timerId = setTimeout ( cb , this . restTime , ...this . arg )
118
39
}
@@ -122,15 +43,15 @@ class Timer {
122
43
if ( this . timeout > 0 ) {
123
44
const now = + new Date ( )
124
45
const nextRestTime = this . restTime - ( now - this . startTime )
125
- this . restTime = this . isInterval ? Math . abs ( nextRestTime ) % this . timeout : nextRestTime
46
+ const intervalRestTime = nextRestTime >= 0 ? nextRestTime : this . timeout - ( Math . abs ( nextRestTime ) % this . timeout )
47
+
48
+ this . restTime = this . isInterval ? intervalRestTime : nextRestTime
126
49
}
127
50
clearTimeout ( this . timerId )
128
51
}
129
52
}
130
53
131
54
/* 定时器增量 id */
132
55
Timer . count = 0
133
- /* 存储页面定时器和页面显示或隐藏状态 */
134
- Timer . pageStore = new Map ( )
135
56
136
57
export { Timer }
0 commit comments