dashboard: pause polling when tab is hidden
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user