From 7bcb670b9671bd877be9d025a6159a3e48e6ccd4 Mon Sep 17 00:00:00 2001 From: koko210Serve Date: Sat, 28 Feb 2026 23:25:07 +0200 Subject: [PATCH] perf: pause polling intervals when browser tab is hidden - Replace raw setInterval with startPolling/stopPolling functions - Add visibilitychange listener to pause when tab is hidden - Immediately refresh data when tab becomes visible again - Saves bandwidth and CPU when the dashboard is in background --- bot/static/index.html | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/bot/static/index.html b/bot/static/index.html index 20c2e8d..b72ba1f 100644 --- a/bot/static/index.html +++ b/bot/static/index.html @@ -1908,10 +1908,33 @@ document.addEventListener('DOMContentLoaded', function() { }, { passive: false }); } - // Set up periodic updates - setInterval(loadStatus, 10000); - setInterval(loadLogs, 5000); - setInterval(loadActiveArguments, 5000); // Refresh active arguments + // Set up periodic updates with visibility-aware polling + let statusInterval, logsInterval, argsInterval; + + function startPolling() { + if (!statusInterval) statusInterval = setInterval(loadStatus, 10000); + if (!logsInterval) logsInterval = setInterval(loadLogs, 5000); + if (!argsInterval) argsInterval = setInterval(loadActiveArguments, 5000); + } + + function stopPolling() { + clearInterval(statusInterval); statusInterval = null; + clearInterval(logsInterval); logsInterval = null; + clearInterval(argsInterval); argsInterval = null; + } + + document.addEventListener('visibilitychange', () => { + if (document.hidden) { + stopPolling(); + console.log('⏸ Tab hidden — polling paused'); + } else { + loadStatus(); loadLogs(); loadActiveArguments(); + startPolling(); + console.log('▶️ Tab visible — polling resumed'); + } + }); + + startPolling(); }); // Utility functions