I have a brief section of JavaScript that I would like to use to check a server every few seconds and update the DOM.
function updateCard() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
card = JSON.parse(this.responseText);
document.getElementById("season").innerHTML = card.season;
}
};
xhttp.open("GET", "/curr_card/", true);
xhttp.send();
}
window.onload = updateCard;
window.setInterval(updateCard,2000);
In most browsers, this is what occurs. There are occasional calls to updateCard
, but generally speaking the server displays around half a connection per second per client.
However, when viewing the page in Firefox on Android (49.0), the browser suddenly starts polling /curr_card/
repeatedly, many times per second.
Some people have suggested replacing the setInterval line with
window.setInterval(function() {updateCard();},2000);
, but this does not solve the issue.
As a newcomer to JavaScript and AJAX, I am unsure why this is happening. Is it a bug in Firefox? I can provide more code upon request.
Thank you in advance.