getting xmlhttprequest to loop continuously

L

libsfan01

Hi all

How can i modify my code so that js will continously check the data on
a page (data.php) and bring it through. at the moment it only does it
once when the function is called, how can i modify my code to allow for
continous looping?

function getdata() {

get = new XMLHttpRequest();
get.onreadystatechange = processdata;
get.open("GET", "data.php");
get.send(null);

}

function processdata() {

if (get.readyState == 4) {
data = get.responseText;
document.getElementById("display").value = data;
}

}
 
P

pleaseexplaintome

I don't know php, but how about a while loop? you might need a sleep
call as well.

while (1=1)
{
execute code....
}

while (true)
{
execute code....
}
 
L

Laurent Bugnion

Hi,

I don't know php, but how about a while loop? you might need a sleep
call as well.

while (1=1)
{
execute code....
}

while (true)
{
execute code....
}

First, the code is JavaScript, not PHP. The OP has a PHP page that he is
calling using a XmlHttpRequest object. The call is made in JavaScript.

Second, a while with a web request? Bad idea. Your code will (attempt
to) send tons of HTTP requests. I say attempt to, because obviously that
will fail. Best way to do what the OP wants is to send a request, wait
calmly for the response, handle the response, and then send a new
request (after possibly a little wait).

Third, you cannot sleep in JavaScript. You can schedule an action using
setTimeout if needed.

HTH,
Laurent
 
L

Laurent Bugnion

Hi,

How about setInterval() ?
I'm using it, in a Azureus Webplugin, works fine!

setInterval in the context of a web request is not recommended, because
you have no idea how long the web request is actually going to take. If
the server is overloaded, traffic is slow, the request takes time to be
executed, etc... then the Response will be delayed. There is a risk that
you'll send a new request before the old one is completed.

This is why I recommend a sequential approach: Send the request, wait
for the response, then use setTimeout to avoid overloading the server
with requests too close from another.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top