// common.js - 通用功能和初始化函数 // 计时器相关变量 let timerInterval = null; let timerTotalSeconds = 600; // 默认10分钟 let timerCurrentSeconds = 600; let timerPaused = false; // 在页面加载完成后初始化 document.addEventListener('DOMContentLoaded', function () { // 初始化页面 initializePage(); }); // 标签页切换功能 document.querySelectorAll('.tab').forEach(tab => { tab.addEventListener('click', function () { // 切换标签页 const tabId = this.getAttribute('data-tab'); document.querySelectorAll('.tab').forEach(t => t.classList.remove('active')); document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active')); this.classList.add('active'); document.getElementById(tabId).classList.add('active'); }); }); function initializePage() { showToast('加载主架构....', 'success'); // 初始化时启动时间更新 updateCurrentTime(); loadTimerSettings(); showToast('初始化页面....', 'success'); // 各页面初始化 initializeRepeatabilityPage(); initializeStabilityPage(); initializeErrorPage(); initializeDataPage(); showToast('加载完成', 'success'); } function updateProjectTitle() { const titleInput = document.getElementById('project-title'); const title = titleInput.value.trim() || '无标题'; document.title = `${title} - LabStatistics`; setLocalStorage('projectTitle', title); } // 更新当前时间显示 function updateCurrentTime() { const now = new Date(); const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' }); document.getElementById('current-time').textContent = timeString; setTimeout(updateCurrentTime, 1000); } // 记录时间并执行计算 function recordTimeAndCalculate(type, index, timesArray, calculateFunction) { const now = new Date(); const timestamp = now.getTime(); // 获取时间戳 const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' }); // 更新时间显示 document.getElementById(`${type}-time-text-${index}`).textContent = `${timeString}: `; // 记录测试时间戳 timesArray[index - 1] = timestamp; // 执行计算 if (typeof calculateFunction === 'function') { calculateFunction(); } } // 加载计时器设置 function loadTimerSettings() { const savedMinutes = getLocalStorage('timerMinutes'); const savedSeconds = getLocalStorage('timerSeconds'); if (savedMinutes !== null && savedSeconds !== null) { const minutes = parseInt(savedMinutes); const seconds = parseInt(savedSeconds); document.getElementById('timer-minutes').value = minutes; document.getElementById('timer-seconds').value = seconds; timerTotalSeconds = minutes * 60 + seconds; timerCurrentSeconds = timerTotalSeconds; updateTimerDisplay(); } } // 保存计时器设置 function saveTimerSettings(minutes, seconds) { setLocalStorage('timerMinutes', minutes, 30); setLocalStorage('timerSeconds', seconds, 30); } // 打开计时器对话框 function openTimerDialog() { document.getElementById('timer-dialog').style.display = 'flex'; loadTimerSettings(); } // 关闭计时器对话框 function closeTimerDialog() { document.getElementById('timer-dialog').style.display = 'none'; } // 开始计时器 function startTimer() { if (timerCurrentSeconds <= 0) { return; } // 如果已暂停,则继续计时 if (timerPaused) { timerPaused = false; document.querySelector('#timer-dialog button:nth-child(1)').textContent = '暂停'; } else { if (document.querySelector('#timer-dialog button:nth-child(1)').textContent == '开始') { // 获取设置的时间 const minutes = parseInt(document.getElementById('timer-minutes').value) || 0; const seconds = parseInt(document.getElementById('timer-seconds').value) || 0; timerTotalSeconds = minutes * 60 + seconds; timerCurrentSeconds = timerTotalSeconds; // 保存设置到localStorage saveTimerSettings(minutes, seconds); // 停止警报铃声 document.getElementById('alertSound').pause(); document.getElementById('alertSound').currentTime = 0; // 更新暂停按钮文本为暂停 document.querySelector('#timer-dialog button:nth-child(1)').textContent = '暂停'; } else { if (timerInterval) { clearInterval(timerInterval); timerInterval = null; timerPaused = true; // 停止警报铃声 document.getElementById('alertSound').pause(); document.getElementById('alertSound').currentTime = 0; // 更新暂停按钮文本为继续 document.querySelector('#timer-dialog button:nth-child(1)').textContent = '继续'; } } } closeTimerDialog(); // 更新显示 updateTimerDisplay(); // 开始倒计时 if (!timerInterval && !timerPaused) { timerInterval = setInterval(updateTimer, 1000); } } // 重置计时器 function resetTimer() { if (timerInterval) { clearInterval(timerInterval); timerInterval = null; } timerPaused = false; // 获取设置的时间 const minutes = parseInt(document.getElementById('timer-minutes').value) || 0; const seconds = parseInt(document.getElementById('timer-seconds').value) || 0; timerTotalSeconds = minutes * 60 + seconds; timerCurrentSeconds = timerTotalSeconds; // 更新显示 updateTimerDisplay(); // 停止警报铃声 document.getElementById('alertSound').pause(); document.getElementById('alertSound').currentTime = 0; // 更新暂停按钮文本为开始 document.querySelector('#timer-dialog button:nth-child(1)').textContent = '开始'; } // 更新计时器 function updateTimer() { if (timerCurrentSeconds > 0) { timerCurrentSeconds--; updateTimerDisplay(); // 当倒计时接近结束时播放警报 if (timerCurrentSeconds <= 3 && timerCurrentSeconds > 0) { document.getElementById('alertSound').play(); } } else { // 倒计时结束 clearInterval(timerInterval); timerInterval = null; // 播放警报 document.getElementById('alertSound').play(); } } // 更新计时器显示 function updateTimerDisplay() { const minutes = Math.floor(timerCurrentSeconds / 60); const seconds = timerCurrentSeconds % 60; const displayText = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; document.getElementById('countdown').textContent = displayText; } // 自定义参数输入行创建函数 function createInputRow(index, type, onInputFunction, onDeleteFunction) { const row = document.createElement('div'); row.className = 'input-row'; const timeSpan = document.createElement('span'); timeSpan.id = `${type}-time-text-${index}`; timeSpan.textContent = `${index}: `; const input = document.createElement('input'); input.type = 'number'; input.placeholder = '输入值'; input.id = `${type}-value-${index}`; input.oninput = function () { onInputFunction(index); }; const deleteBtn = document.createElement('div'); deleteBtn.className = 'delete-btn'; deleteBtn.innerHTML = '×'; deleteBtn.onclick = function () { onDeleteFunction(this); }; row.appendChild(timeSpan); row.appendChild(input); row.appendChild(deleteBtn); return row; } function getNumericInputs(selector) { return Array.from(document.querySelectorAll(selector)) .map(input => parseFloat(input.value)) .filter(value => !isNaN(value)); } function updateResultDisplay(id, value, precision = 6) { const element = document.getElementById(id); if (element) { if (value === null || value === undefined || isNaN(value)) { element.innerText = '-'; } else { // 格式化数字,去除尾随的0 const formatted = parseFloat(value.toFixed(precision)).toString(); element.innerText = formatted; } } } function createOrUpdateChart(chartInstance, ctx, labels, data, label) { if (chartInstance) { chartInstance.data.labels = labels; chartInstance.data.datasets[0].data = data; chartInstance.update(); return chartInstance; } else { return new Chart(ctx, { type: 'line', data: { labels: labels, datasets: [{ label: label, data: data, borderColor: 'rgba(75, 192, 192, 1)', backgroundColor: 'rgba(75, 192, 192, 0.2)', borderWidth: 2, pointRadius: 5, pointBackgroundColor: 'rgba(75, 192, 192, 1)', tension: 0.1 }] }, options: { responsive: true, maintainAspectRatio: false, scales: { y: { beginAtZero: false } } } }); } } // 收集并保存数据到localStorage function collectAndSaveData(selector, times, resultIds, dataName) { // 收集输入数据 const inputs = Array.from(document.querySelectorAll(selector)).map((input, index) => { return { time: times[index] ? times[index] : null, value: input.value }; }); // 收集结果数据 const results = {}; if (resultIds) { Object.keys(resultIds).forEach(key => { const element = document.getElementById(resultIds[key]); if (element) { results[key] = element.innerText; } }); } // 保存到localStorage const data = { inputs: inputs, results: results }; console.log(dataName, data); setLocalStorage(dataName, JSON.stringify(data)); }