mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 20:11:14 +00:00
Uses timeout instead of interval
This commit is contained in:
parent
94a41498cf
commit
1778e8dd46
1 changed files with 11 additions and 9 deletions
|
@ -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())
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue