I am currently working on a chat application that needs to constantly receive server information. After the request finishes, I have added an additional call to the function within the
http.onreadystatechange=function()
like this:
request();
This sets everything up to run in a loop. However, I am running into an issue where it only seems to work properly in Google Chrome. In Internet Explorer and Firefox, the browser does not wait for the
get.onreadystatechange=function()
to finish before continuously calling return()
multiple times per second, causing unnecessary load on the server.
function request()
{
var get;
if (window.XMLHttpRequest)
{
get = new XMLHttpRequest();
}
document.getElementById("request_status").innerHTML = "requests: "+requests;
get.onreadystatechange = function()
{
if (get.readyState == 4 && get.status == 200)
{
requests += 1;
request();
}
}
get.open("GET", "request.php", true);
get.send();
}
When running in Google Chrome, the 'requests' increase by around 4 per second. However, in Internet Explorer and Firefox, the count jumps up to about 200 per second, indicating that something is not functioning as expected.