From ec7ef6ef1e415981184088fa98cda04931fa95c9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 30 Nov 2021 17:45:56 +0100 Subject: [PATCH] reduce needed cyle when idleing --- .../static/script/inventree/inventree.js | 12 +++++++----- .../static/script/inventree/notification.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/inventree.js b/InvenTree/InvenTree/static/script/inventree/inventree.js index dd9a725fa1..0e7c09c32d 100644 --- a/InvenTree/InvenTree/static/script/inventree/inventree.js +++ b/InvenTree/InvenTree/static/script/inventree/inventree.js @@ -218,12 +218,14 @@ function inventreeDocReady() { // Display any cached alert messages showCachedAlerts(); - // Start notification background worker to check every 1 seconds if notifications are available - setInterval(notificationCheck, 1000); + // always refresh when the focus returns + $(document).focus(function(){ + startNotificationWatcher(); + }); - // also run when the focus returns - $(document).on('focus', function(e){ - notificationCheck(force = true); + // kill notification watcher if focus is lost -> respect your users cycles + $(document).focusout(function(){ + stopNotificationWatcher(); }); $('#offcanvasRight').on('show.bs.offcanvas', openNotificationPanel) // listener for opening the notification panel diff --git a/InvenTree/InvenTree/static/script/inventree/notification.js b/InvenTree/InvenTree/static/script/inventree/notification.js index c5020169d6..4fc9b0ac77 100644 --- a/InvenTree/InvenTree/static/script/inventree/notification.js +++ b/InvenTree/InvenTree/static/script/inventree/notification.js @@ -119,6 +119,23 @@ function showMessage(message, options={}) { }); } +var notificationWatcher = null; // reference for the notificationWatcher +/** + * start the regular notification checks + **/ +function startNotificationWatcher() { + notificationCheck(force=true); + notificationWatcher = setInterval(notificationCheck, 1000); +} + +/** + * stop the regular notification checks + **/ +function stopNotificationWatcher() { + clearInterval(notificationWatcher); +} + + var notificationUpdateTic = 0; /** * The notification checker is initiated when the document is loaded. It checks if there are unread notifications