From 865efe41f7d4a66a1160dcbf339445df4fe06477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 18 Sep 2017 00:27:47 +0200 Subject: [PATCH] visibility api --- js/term_conn.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/js/term_conn.js b/js/term_conn.js index e6276c8..e74ade6 100644 --- a/js/term_conn.js +++ b/js/term_conn.js @@ -19,15 +19,16 @@ module.exports = class TermConnection extends EventEmitter { this.pageShown = false - window.addEventListener('focus', () => { - console.info('Window got focus, re-connecting') - this.init() - }) - window.addEventListener('blur', () => { - console.info('Window lost focus, freeing socket') - this.closeSocket() - clearTimeout(this.heartbeatTimeout) - }) + document.addEventListener('visibilitychange', () => { + if (document.hidden === true) { + console.info('Window lost focus, freeing socket') + this.closeSocket() + clearTimeout(this.heartbeatTimeout) + } else { + console.info('Window got focus, re-connecting') + this.init() + } + }, false) } onWSOpen (evt) {