| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- // stability.js - 稳定性测试相关功能
- // 稳定性测试时间数组
- let stabilityTimes = [];
- // 图表相关变量
- let stabilityCtx = null;
- let stabilityChartInstance = null;
- // 计时器相关变量
- let timerInterval = null;
- let timerTotalSeconds = 600; // 默认10分钟
- let timerCurrentSeconds = 600;
- let timerPaused = false;
- // 初始化稳定性图表
- function initStabilityChart() {
- stabilityCtx = document.getElementById('stabilityChart').getContext('2d');
- }
- // 在页面加载完成后初始化图表
- document.addEventListener('DOMContentLoaded', function() {
- initStabilityChart();
- });
- // 更新稳定性输入框的占位符
- function updateStabilityInputPlaceholders() {
- document.querySelectorAll('#stability-inputs input[type="number"]').forEach((input, index) => {
- input.placeholder = `测量值 ${index + 1}`;
- });
- }
- // 更新稳定性输入框的删除按钮
- function updateStabilityDeleteButtons() {
- const stabilityInputs = document.querySelectorAll('#stability-inputs .input-row');
- stabilityInputs.forEach((row, index) => {
- const deleteBtn = row.querySelector('.delete-btn');
- if (deleteBtn) {
- deleteBtn.style.display = index < 6 ? 'none' : 'inline';
- }
- });
- }
- // 稳定性测试函数
- function addStabilityInput() {
- const container = document.getElementById('stability-inputs');
- const newIndex = container.children.length + 1;
- const row = createInputRow(newIndex, 'stability', recordTimeAndCalculateStability, deleteStabilityInput);
-
- // 设置默认时间为上一个时间+10分钟
- const lastTimeSpan = container.querySelector(`.input-row:last-child span[id^="stability-time-text-"]`);
- let lastTime = 0;
- if (lastTimeSpan) {
- const lastTimeText = lastTimeSpan.textContent.trim();
- lastTime = parseInt(lastTimeText) || 0;
- }
- const newTime = lastTime + 10;
- row.querySelector(`span[id^="stability-time-text-"]`).textContent = `${newTime}: `;
-
- container.appendChild(row);
- updateStabilityInputs();
- }
- function deleteStabilityInput(btn) {
- const row = btn.parentNode;
- const container = row.parentNode;
- const index = Array.from(container.children).indexOf(row);
-
- // 移除对应的时间记录
- stabilityTimes.splice(index, 1);
-
- // 移除输入行
- container.removeChild(row);
-
- // 更新剩余输入行的索引
- const rows = container.querySelectorAll('.input-row');
- rows.forEach((row, i) => {
- const timeSpan = row.querySelector(`span[id^="stability-time-text-"]`);
- const input = row.querySelector(`input[id^="stability-value-"]`);
-
- // 更新ID
- const newIndex = i + 1;
- timeSpan.id = `stability-time-text-${newIndex}`;
- input.id = `stability-value-${newIndex}`;
-
- // 更新输入事件
- input.oninput = function() { recordTimeAndCalculateStability(newIndex); };
- });
-
- updateStabilityInputs();
- calculateStability();
- saveStabilityData();
- }
- function recordTimeAndCalculateStability(index) {
- recordTimeAndCalculate('stability', index, stabilityTimes, calculateStability);
- }
- function calculateStability() {
- const values = getNumericInputs('#stability-inputs input[type="number"]');
-
- if (values.length >= 2) {
- // 计算统计量
- const max = Math.max(...values);
- const min = Math.min(...values);
- const range = max - min;
- const mean = values.reduce((sum, val) => sum + val, 0) / values.length;
-
- // 更新结果显示
- updateResultDisplay('stability-max', max);
- updateResultDisplay('stability-min', min);
- updateResultDisplay('stability-mean', mean);
- updateResultDisplay('stability-range', range);
-
- // 更新图表
- updateStabilityChart(values);
- } else {
- // 清空结果显示
- document.querySelectorAll('#stability .result-item span').forEach(span => {
- span.innerText = '-';
- });
-
- // 清空图表
- if (stabilityChartInstance) {
- stabilityChartInstance.data.labels = [];
- stabilityChartInstance.data.datasets[0].data = [];
- stabilityChartInstance.update();
- }
- }
- }
- function updateStabilityChart(values) {
- // 获取时间标签
- const timeLabels = [];
- document.querySelectorAll('#stability-inputs .input-row').forEach((row, index) => {
- const timeSpan = row.querySelector(`span[id^="stability-time-text-"]`);
- const timeText = timeSpan.textContent.trim();
- const time = timeText.replace(':', '');
- timeLabels.push(time);
- });
-
- // 使用通用图表创建函数
- stabilityChartInstance = createOrUpdateChart(stabilityChartInstance, stabilityCtx, timeLabels, values, '稳定性');
- }
- function saveStabilityData() {
- collectAndSaveData('#stability-inputs input[type="number"]', 'stability-time-text', {
- max: 'stability-max',
- min: 'stability-min',
- mean: 'stability-mean',
- range: 'stability-range'
- }, 'stabilityData');
- }
- // 加载计时器设置
- function loadTimerSettings() {
- const savedMinutes = getLocalStorage('timerMinutes');
- const savedSeconds = getLocalStorage('timerSeconds');
- const savedAutoCountdown = getLocalStorage('autoCountdownEnabled');
- 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();
- }
- // 加载自动倒计时设置
- if (savedAutoCountdown !== null) {
- document.getElementById('auto-countdown-enabled').checked = (savedAutoCountdown === '1');
- }
- }
- // 保存计时器设置
- function saveTimerSettings(minutes, seconds) {
- setLocalStorage('timerMinutes', minutes, 30);
- setLocalStorage('timerSeconds', seconds, 30);
- // 保存自动倒计时设置
- const autoCountdownEnabled = document.getElementById('auto-countdown-enabled').checked;
- setLocalStorage('autoCountdownEnabled', autoCountdownEnabled ? '1' : '0', 30);
- }
- // 打开计时器对话框
- function openTimerDialog() {
- document.getElementById('timer-dialog').style.display = 'flex';
- }
- // 关闭计时器对话框
- function closeTimerDialog() {
- document.getElementById('timer-dialog').style.display = 'none';
- }
- // 开始计时器
- function startTimer() {
- // 如果已暂停,则继续计时
- if (timerPaused) {
- timerPaused = false;
- } else {
- // 获取设置的时间
- const minutes = parseInt(document.getElementById('timer-minutes').value) || 0;
- const seconds = parseInt(document.getElementById('timer-seconds').value) || 0;
- timerTotalSeconds = minutes * 60 + seconds;
- timerCurrentSeconds = timerTotalSeconds;
- // 保存设置到cookie
- saveTimerSettings(minutes, seconds);
- // 停止警报铃声
- document.getElementById('alertSound').pause();
- document.getElementById('alertSound').currentTime = 0;
- // 更新暂停按钮文本为暂停
- document.querySelector('#timer-dialog button:nth-child(1)').textContent = '暂停';
- }
- // 更新显示
- updateTimerDisplay();
- // 开始倒计时
- if (!timerInterval) {
- timerInterval = setInterval(updateTimer, 1000);
- }
- }
- // 暂停计时器
- function pauseTimer() {
- 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 = '继续';
- }
- }
- // 重置计时器
- 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();
- // 更新暂停按钮文本为开始
- document.querySelector('#timer-dialog button:nth-child(1)').textContent = '开始';
- }
- }
- // 更新计时器显示
- 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;
- }
|