dashboard: pause polling when tab is hidden
All checks were successful
CI / syntax-check (push) Successful in 2s
CI / memory-leak-check (push) Successful in 11s

This commit is contained in:
Username
2025-12-26 19:58:20 +01:00
parent ad89eb262e
commit 1f41f3df5c
2 changed files with 35 additions and 2 deletions

View File

@@ -662,5 +662,37 @@ function fetchStats() {
.catch(function(e) { $('dot').className = 'dot err'; $('statusTxt').textContent = 'Error'; });
}
fetchStats();
setInterval(fetchStats, 3000);
// Visibility-aware polling - pause when tab is hidden
var pollInterval = null;
var pollDelay = 3000;
function startPolling() {
if (!pollInterval) {
fetchStats(); // Fetch immediately
pollInterval = setInterval(fetchStats, pollDelay);
}
}
function stopPolling() {
if (pollInterval) {
clearInterval(pollInterval);
pollInterval = null;
$('dot').className = 'dot warn';
$('statusTxt').textContent = 'Paused';
}
}
function handleVisibilityChange() {
if (document.hidden) {
stopPolling();
} else {
startPolling();
}
}
document.addEventListener('visibilitychange', handleVisibilityChange);
// Initial start
if (!document.hidden) {
startPolling();
}