chenhaozhe
3 天以前 cfa35ff1f39c2a7b69d624d45f8e05591f3375d4
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
/**
 * Layui 扩展模块:定时器管理器
 * 功能:统一管理定时器,支持暂停/恢复/清除,防止内存泄漏
 */
layui.define([], function(exports) {
    "use strict";
 
    // 原定时器核心类
    class TimerManager {
        constructor() {
            this.timers = new Map();
        }
 
        /**
         * 创建定时器
         * @param {String} key 定时器唯一标识(如:'countdown')
         * @param {Function} callback 执行的业务函数
         * @param {Number} interval 执行间隔(ms)
         * @param {String} type 类型:interval(重复)/ timeout(单次)
         * @returns {void}
         */
        createTimer(key, callback, interval = 1000, type = 'interval') {
            // 先清除同名定时器,避免重复创建
            this.clearTimer(key);
 
            let timerId = null;
            const timerConfig = {
                id: null,
                type,
                callback,
                interval,
                isPaused: false
            };
 
            if (type === 'interval') {
                timerId = setInterval(() => {
                    // 暂停状态不执行
                    if (!timerConfig.isPaused) {
                        callback();
                    }
                }, interval);
            } else if (type === 'timeout') {
                timerId = setTimeout(() => {
                    if (!timerConfig.isPaused) {
                        callback();
                        this.clearTimer(key); // 单次执行后自动清除
                    }
                }, interval);
            }
 
            timerConfig.id = timerId;
            this.timers.set(key, timerConfig);
        }
 
        /**
         * 暂停定时器
         * @param {String} key 定时器标识
         */
        pauseTimer(key) {
            const timer = this.timers.get(key);
            if (timer) {
                timer.isPaused = true;
            }
        }
 
        /**
         * 恢复定时器
         * @param {String} key 定时器标识
         */
        resumeTimer(key) {
            const timer = this.timers.get(key);
            if (timer) {
                timer.isPaused = false;
            }
        }
 
        /**
         * 清除单个定时器
         * @param {String} key 定时器标识
         */
        clearTimer(key) {
            const timer = this.timers.get(key);
            if (timer) {
                if (timer.type === 'interval') {
                    clearInterval(timer.id);
                } else if (timer.type === 'timeout') {
                    clearTimeout(timer.id);
                }
                this.timers.delete(key);
            }
        }
 
        /**
         * 清除所有定时器(页面销毁时调用)
         */
        clearAllTimers() {
            this.timers.forEach((timer) => {
                if (timer.type === 'interval') {
                    clearInterval(timer.id);
                } else if (timer.type === 'timeout') {
                    clearTimeout(timer.id);
                }
            });
            this.timers.clear();
        }
    }
 
    // 创建单例
    const timerManager = new TimerManager();
 
    // 导出模块
    exports('timerManager', timerManager);
});