From e04371500a9dd469f02024f63ef39117f8a4d649 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 2 Oct 2018 11:30:54 +0300 Subject: js cleanup --- juick-server/src/main/assets/scripts.js | 44 +++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'juick-server') diff --git a/juick-server/src/main/assets/scripts.js b/juick-server/src/main/assets/scripts.js index af099674..8060bd71 100644 --- a/juick-server/src/main/assets/scripts.js +++ b/juick-server/src/main/assets/scripts.js @@ -175,7 +175,11 @@ function initWS() { } } }; - setInterval(wsSendKeepAlive, 90000); + var keepAlive = setInterval(wsSendKeepAlive, 90000); + window.addEventListener('beforeunload', () => { + clearInterval(keepAlive); + ws.close(); + }) } function wsSendKeepAlive() { @@ -277,7 +281,7 @@ function postformListener(formEl, ev) { if (ev.ctrlKey && (ev.keyCode == 10 || ev.keyCode == 13)) { let form = formEl.closest('form'); if (!form.onsubmit || form.onsubmit()) { - form.dispatchEvent(new Event('submit')); + form.querySelector('input[type="submit"]').click(); } } } @@ -300,12 +304,19 @@ function newMessage(evt) { } } +function handleErrors(response) { + if (!response.ok) { + throw Error(response.statusText); + } + return response; +} + function showCommentForm(mid, rid) { let reply = document.getElementById(rid); let formTarget = reply.querySelector('div.msg-cont .msg-comment-target'); if (formTarget) { let formHtml = ` -