From de852e30138bd921fbb3fef55c09e88b25084b1a Mon Sep 17 00:00:00 2001 From: Gabriele Svelto Date: Thu, 14 Jan 2016 12:33:06 +0100 Subject: [PATCH] Do not refresh the display when the app is hidden. Fix #14. --- js/app.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/js/app.js b/js/app.js index a680f13..bdf8858 100755 --- a/js/app.js +++ b/js/app.js @@ -9,6 +9,7 @@ var accounts; var lis = []; // list items for accounts. var refreshInterval; + var refreshing = false; // Indicates whether we should refresh the page /* Shortcuts */ var $ = document.querySelector.bind(document); @@ -67,6 +68,12 @@ /* Main screen */ function refreshAccounts() { + refreshing = true; + + if (document.hidden) { + return; // Do not refresh when hidden + } + // Let's do this again sometime. if (accounts && !refreshInterval) { refreshInterval = window.setInterval(refreshAccounts, 1000); @@ -112,10 +119,23 @@ } function stopUpdating() { + refreshing = false; window.clearInterval(refreshInterval); refreshInterval = null; } + window.addEventListener('visibilitychange', function(evt) { + if (document.hidden) { + window.clearInterval(refreshInterval); + refreshInterval = null; + } else { + if (refreshing) { + // Resume refreshing only if we hadn't stopped before + refreshAccounts(); + } + } + }); + // Long click for delete. $('#main .delete').addEventListener('click', function() { var delButton = this;