diff options
author | Simon Knox <psimyn@gmail.com> | 2017-10-24 18:58:05 +0300 |
---|---|---|
committer | Simon Knox <psimyn@gmail.com> | 2017-10-24 19:06:20 +0300 |
commit | 9101cce2a7c30a832b3d97d84cd53ca503a163d1 (patch) | |
tree | 0d9bddbef888053e22812f1cafab51e47768c929 /app/assets/javascripts/smart_interval.js | |
parent | 2087f121bfedd2f51eb943f23026e823d196466a (diff) |
don't re-run smart interval callback if there is already one in progress
because some things take time
Diffstat (limited to 'app/assets/javascripts/smart_interval.js')
-rw-r--r-- | app/assets/javascripts/smart_interval.js | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/app/assets/javascripts/smart_interval.js b/app/assets/javascripts/smart_interval.js index 2bf7a3a5d61..cee2d2ba1e2 100644 --- a/app/assets/javascripts/smart_interval.js +++ b/app/assets/javascripts/smart_interval.js @@ -42,13 +42,16 @@ class SmartInterval { const cfg = this.cfg; const state = this.state; - if (cfg.immediateExecution) { + if (cfg.immediateExecution && !this.isLoading) { cfg.immediateExecution = false; - cfg.callback(); + this.triggerCallback(); } state.intervalId = window.setInterval(() => { - cfg.callback(); + if (this.isLoading) { + return; + } + this.triggerCallback(); if (this.getCurrentInterval() === cfg.maxInterval) { return; @@ -76,7 +79,7 @@ class SmartInterval { // start a timer, using the existing interval resume() { - this.stopTimer(); // stop exsiting timer, in case timer was not previously stopped + this.stopTimer(); // stop existing timer, in case timer was not previously stopped this.start(); } @@ -104,6 +107,17 @@ class SmartInterval { this.initPageUnloadHandling(); } + triggerCallback() { + this.isLoading = true; + this.cfg.callback() + .then(() => { + this.isLoading = false; + }) + .catch(() => { + this.isLoading = false; + }); + } + initVisibilityChangeHandling() { // cancel interval when tab no longer shown (prevents cached pages from polling) document.addEventListener('visibilitychange', this.handleVisibilityChange.bind(this)); |