28fc3b409f
- 引入 ScriptManager、TimerManager 和 EventManager 统一管理资源 - 路由配置支持默认重定向路径 '/' - 移除旧版 scriptLoader 与 routeUtils,使用新的模块化加载机制 - 增加路由生命周期控制,自动清理定时器和事件监听器 - 改进点击代理逻辑,增强按钮激活状态切换 - 页面切换时卸载旧脚本及资源,防止内存泄漏 - 修复初始化日志冗余问题,提升代码可维护性
54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
export class TimerManager {
|
|
constructor() {
|
|
// 定时器对象数组
|
|
this.timers = [];
|
|
}
|
|
|
|
/**
|
|
* 添加定时器到数组中
|
|
* @param {*} fn
|
|
* @param {*} delay
|
|
* @param {...any} args
|
|
* @returns
|
|
*/
|
|
setTimeout(fn, delay, ...args) {
|
|
const id = window.setTimeout(fn, delay, ...args);
|
|
this.timers.push(id);
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* 添加定时器到数组中
|
|
* @param {*} fn
|
|
* @param {*} delay
|
|
* @param {...any} args
|
|
* @returns
|
|
*/
|
|
setInterval(fn, delay, ...args) {
|
|
const id = window.setInterval(fn, delay, ...args);
|
|
this.timers.push(id);
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* 清除定时器
|
|
* @param {*} id
|
|
*/
|
|
clearTimeout(id) {
|
|
window.clearTimeout(id);
|
|
this.timers = this.timers.filter((timerId) => timerId !== id);
|
|
}
|
|
|
|
clearInterval(id) {
|
|
window.clearInterval(id);
|
|
this.timers = this.timers.filter((timerId) => timerId !== id);
|
|
}
|
|
|
|
cleanAll() {
|
|
this.timers.forEach((timerId) => {
|
|
window.clearTimeout(timerId);
|
|
window.clearInterval(timerId);
|
|
});
|
|
this.timers = [];
|
|
}
|
|
} |