From 1778e8dd468ff2c28d36b7797408a81915c9af37 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 19 Jan 2021 14:59:46 -0800 Subject: [PATCH] Uses timeout instead of interval --- bookwyrm/static/js/shared.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/bookwyrm/static/js/shared.js b/bookwyrm/static/js/shared.js index 3a56431e6..e8ff9c469 100644 --- a/bookwyrm/static/js/shared.js +++ b/bookwyrm/static/js/shared.js @@ -34,23 +34,25 @@ window.onload = function() { // polling document.querySelectorAll('[data-poll]') - .forEach(t => setInterval(function () { polling(t); }, 10000)); + .forEach(el => polling(el)); }; - function polling(el) { - // poll the endpoint - fetch('/api/updates/' + el.getAttribute('data-poll'), { - method : "GET", - }).then(response => response.json()) - .then(data => updateCountElement(el, data)); + let delay = 10000 + (Math.random() * 1000); + setTimeout(function() { + fetch('/api/updates/' + el.getAttribute('data-poll')) + .then(response => response.json()) + .then(data => updateCountElement(el, data)); + polling(el); + }, delay, el); } + function updateCountElement(el, data) { - const currentCount = el.innerHTML; + const currentCount = el.innerText; const count = data[el.getAttribute('data-poll')]; if (count != currentCount) { addRemoveClass(el, 'hidden', count < 1); - el.innerHTML = count; + el.innerText = count; } }