aaefc9c3fc
- 引入 BaseComponent 基类统一管理事件、定时器和脚本加载 - 重构 timestamp 模块为继承自 BaseComponent 的类组件 - 新增 ScriptManager 工具类用于动态加载和卸载脚本 - 修改路由配置支持按需加载页面对应的 JS 模块 - 路由切换时自动销毁旧组件实例以防止内存泄漏 - 更新文件引用路径适配新的目录结构 - 修复 rollup 配置中的语法问题及端口配置 - 移除无用的日志输出和冗余代码提升性能
55 lines
1.1 KiB
JavaScript
55 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 = [];
|
|
}
|
|
}
|