From a950ad136edf739d3d0e846e6398df46475c284d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 28 Sep 2018 12:36:36 +0200 Subject: [PATCH] do not refresh table when tab is invisible or user idle (#26804) * refresh timer is cleared on tab invisible event * refresh timer is cleared after 5 minutes of inactivity * table is refreshed immediately when tab become visible and user has been inactive more than 5 minutes --- wcs/qommon/static/js/wcs.listing.js | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/wcs/qommon/static/js/wcs.listing.js b/wcs/qommon/static/js/wcs.listing.js index c078bf00..9386c612 100644 --- a/wcs/qommon/static/js/wcs.listing.js +++ b/wcs/qommon/static/js/wcs.listing.js @@ -1,3 +1,26 @@ +/** + * vis() -> bool : is tab visible ? + * vis(aFunction) -> call aFunction on visibility change. + */ +var vis = (function(){ + var stateKey, eventKey, keys = { + hidden: "visibilitychange", + webkitHidden: "webkitvisibilitychange", + mozHidden: "mozvisibilitychange", + msHidden: "msvisibilitychange" + }; + for (stateKey in keys) { + if (stateKey in document) { + eventKey = keys[stateKey]; + break; + } + } + return function(c) { + if (c) document.addEventListener(eventKey, c); + return !document[stateKey]; + } +})(); + function prepare_page_links() { $('#page-links a').click(function() { $('form#listing-settings input[name="offset"]').val($(this).data('offset')); @@ -190,9 +213,28 @@ $(function() { if ($('#statistics').length == 0) { var idle_id = null; + var longidle_id = null; + vis(function () { + if (! vis()) { + /* stop refreshing in invisible tabs */ + if (idle_id) idle_id = window.clearInterval(idle_id); + } else { + /* if tab invisible for more than 5 minutes, refresh now */ + if (! longidle_id) autorefresh_table(); + } + } $(window).on('mousemove keydown mousedown touchstart', function() { + /* if refresh timer exists, clear it */ if (idle_id) window.clearInterval(idle_id); + /* if stop refresh timer exists, clear it */ + if (longidle_id) window.clearTimeout(longidle_id); + /* launch timer to refresh every 30 seconds */ idle_id = setInterval(autorefresh_table, 30000); + /* launch timer to stop refresh after 5 minutes idle */ + longidle_id = setTimeout(function () { + if (idle_id) idle_id = window.clearInterval(idle_id); + longidle_id = undefined; + }, 300 * 1000); }); } -- 2.20.1