diff --git a/package.json b/package.json index 7a68ff6d..53d3317a 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "vue-form-wizard": "0.8.4", "vue-i18n": "8.22.2", "vue-loader": "^15.9.6", + "vue-observe-visibility": "^1.0.0", "vue-perfect-scrollbar": "0.2.1", "vue-prism-component": "1.1.1", "vue-qr": "^2.5.0", diff --git a/src/main.js b/src/main.js index 6d720bde..2b235983 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,5 @@ import Vue from 'vue' +import { ObserveVisibility } from 'vue-observe-visibility' import { ToastPlugin, ModalPlugin } from 'bootstrap-vue' import VueCompositionAPI from '@vue/composition-api' import messages from '@/lang' @@ -19,6 +20,7 @@ import '@/libs/portal-vue' import '@/libs/toastification' import '@/libs/clipboard' +Vue.directive('observe-visibility', ObserveVisibility) // Vue.use(VueGtag, { config: { id: 'UA-238887-1' } }, router) Vue.use(VueI18n) diff --git a/src/views/Home.vue b/src/views/Home.vue index 436284ec..7d6f00d7 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -40,6 +40,7 @@ @@ -150,29 +151,27 @@ export default { return this.downImg }, }, - created() { - this.fetch() - this.timer = setInterval(this.fetch, 120000) - }, - beforeDestroy() { - clearInterval(this.timer) - }, methods: { - fetch() { - Object.keys(this.chains).forEach(k => { - const chain = this.chains[k] + fetch(k) { + const chain = this.chains[k] + if (chain.api) { const index = localStorage.getItem(`${chain.chain_name}-api-index`) || 0 - if (chain.api) { - const host = Array.isArray(chain.api) ? chain.api[index] : chain.api - fetch(`${host}/blocks/latest`).then(res => res.json()).then(b => { - // console.log(b.block.header) - const { header } = b.block - this.$set(chain, 'height', header.height) - this.$set(chain, 'time', toDay(header.time)) - this.$set(chain, 'variant', timeIn(header.time, 3, 'm') ? 'danger' : 'success') - }) - } - }) + const host = Array.isArray(chain.api) ? chain.api[index] : chain.api + fetch(`${host}/blocks/latest`).then(res => res.json()).then(b => { + const { header } = b.block + this.$set(chain, 'height', header.height) + this.$set(chain, 'time', toDay(header.time)) + this.$set(chain, 'variant', timeIn(header.time, 3, 'm') ? 'danger' : 'success') + }) + } + }, + visibilityChanged(isVisible, chain) { + this.isVisible = isVisible + const idle = this.chains[chain.chain_name] + if (isVisible && !idle.loaded) { + this.$set(idle, 'loaded', true) + this.fetch(chain.chain_name) + } }, }, } diff --git a/yarn.lock b/yarn.lock index abeb6796..593ef445 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9909,6 +9909,11 @@ vue-loader@^15.9.6: vue-hot-reload-api "^2.3.0" vue-style-loader "^4.1.0" +vue-observe-visibility@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/vue-observe-visibility/-/vue-observe-visibility-1.0.0.tgz#17cf1b2caf74022f0f3c95371468ddf2b9573152" + integrity sha512-s5TFh3s3h3Mhd3jaz3zGzkVHKHnc/0C/gNr30olO99+yw2hl3WBhK3ng3/f9OF+qkW4+l7GkmwfAzDAcY3lCFg== + vue-perfect-scrollbar@0.2.1: version "0.2.1" resolved "https://registry.npmjs.org/vue-perfect-scrollbar/-/vue-perfect-scrollbar-0.2.1.tgz"