feat: 新增倒计时功能#47
以下内容由 AI 生成
新增侧边栏 CNB 面板及回收倒计时功能,优化文件树样式并扩展环境变量
CodeBuddy Code
问题: 定时器内存泄漏风险:handleCountdown 函数中使用 setInterval 创建定时器,但没有保存返回值,也没有提供清理机制。当侧边栏被反复重新加载时,可能会累积多个定时器,导致内存泄漏和性能问题。 建议保存定时器 ID 并在适当时机清理:
handleCountdown
setInterval
function handleCountdown() { // ... 现有代码 ... let timerId = null; function update() { // ... 现有逻辑 ... } update(); timerId = setInterval(update, 1000); // 返回清理函数,供外部调用 return function cleanup() { if (timerId) { clearInterval(timerId); timerId = null; } }; }
或者在 window.onunload 时清理定时器。
window.onunload
问题: 日期解析异常处理缺失:new Date(buildStartTime).getTime() 可能返回 NaN,当 buildStartTime 为无效日期格式时,后续所有时间计算都会失败,导致显示 NaN:NaN:NaN 等异常结果。 建议增加日期有效性校验:
new Date(buildStartTime).getTime()
NaN
buildStartTime
NaN:NaN:NaN
const startMs = new Date(buildStartTime).getTime(); // 验证日期是否有效 if (isNaN(startMs)) { section.style.display = 'none'; console.warn('Invalid buildStartTime format:', buildStartTime); return; } const endMs = startMs + maxRunTime;
问题: 边界值处理不够健壮:
Number()
!maxRunTime
maxRunTime
const maxRunTime = Number(section.getAttribute('data-max-run-time')); const buildStartTime = section.getAttribute('data-build-start-time'); // 更严格的校验 if (!maxRunTime || isNaN(maxRunTime) || maxRunTime <= 0 || !buildStartTime) { section.style.display = 'none'; return; }
新增侧边栏 CNB 面板及回收倒计时功能,优化文件树样式并扩展环境变量