whyczyk
2021-09-07 69f949b70f1cd2c80a9738afe602905b18e72e0b
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
// 延时计时器
function Timeout() {
    this.timer = null;
    this.time = '';
    this.callback = '';
}
// 开启计时器并添加
Timeout.prototype.start = function(callback, time, exe) {
    // 先关闭计时器
    this.stop();
    // 配置执行函数
    if(typeof callback == 'function' && typeof time == 'number') {
        this.callback = callback;
        this.time = time;
        if(exe != 'exe') {
            callback();
        }
        this.timer = setTimeout(callback, time);
    }else {
        // console.trace('timeout');
        // debugger;
        console.warn('未完整配置参数!');
    }
};
// 设置循环体 但不开始
Timeout.prototype.init = function (callback, time) {
    this.callback = callback;
    this.time = time;
}
// 开启计时器
Timeout.prototype.open = function() {
    var callback = this.callback;
    var time = this.time;
    this.start(callback, time, 'exe');
};
 
// 关闭计时器
Timeout.prototype.stop = function() {
    clearTimeout(this.timer);
};
 
export default Timeout;