whyczyk
2021-09-30 c04569e75ef0c526f7f08169cc5724c83005d250
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
// 延时计时器
function Timeout(name) {
    this.timer = null;
    this.time = '';
    this.callback = '';
    this.workState = false;
    this.name = name?name:-33;
}
// 开启计时器并添加
Timeout.prototype.start = function(callback, time, exe) {
    // 配置执行函数
    if(typeof callback == 'function' && typeof time == 'number') {
        this.callback = callback;
        this.time = time;
        // 获取缓存的session
        let name = sessionStorage.getItem('acTabs');
        if(exe != 'exe') {
            this.workState = true;
            callback();
        }else {
            // 已经停止了
            if(!this.workState) {
                return;
            }
            // 清除计时器
            clearTimeout(this.timer);
            if(this.name == -33 || this.name == name) {
                this.timer = setTimeout(callback, time);
            }else {
                setTimeout(()=>{
                    this.open();
                }, 300);
            }
            
            
        }
    }else {
        console.warn('未完整配置参数!');
    }
};
// 开启计时器
Timeout.prototype.open = function() {
    var callback = this.callback;
    var time = this.time;
    this.start(callback, time, 'exe');
};
 
// 关闭计时器
Timeout.prototype.stop = function() {
    clearTimeout(this.timer);
    this.workState = false;
};
 
// 设置循环体 但不开始
Timeout.prototype.init = function (callback, time) {
    this.callback = callback;
    this.time = time;
    this.workState = true;
};
 
// 关闭后重启计时器
Timeout.prototype.restart = function() {
    this.workState = true;
    this.open();
};
 
export default Timeout;