getting XMLHttpRequest to loop continously to check for changes in an xml file

L

libsfan01

Hi all

I want to have create a js function to use xmlhttprequest continously
check a given url for any change in its value and then bring the
contents of that page through.

Here is my code so far, it just pulls is through once when the function
is called, i would like it to work continuously:

function getdata() {

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

}

function processdata() {

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

}


Thanks

Marc
 
L

Laurent Bugnion

Hi,
Hi all

I want to have create a js function to use xmlhttprequest continously
check a given url for any change in its value and then bring the
contents of that page through.

Here is my code so far, it just pulls is through once when the function
is called, i would like it to work continuously:

function getdata() {

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

}

function processdata() {

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

}


Thanks

Marc

Simply call the getdata() function in processdata()

function processdata() {

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

}

Note: You should plan error handling. For example, what do you do if the
status is not 200?

HTH,
Laurent
 
L

libsfan01

Thanks for the help Laurent

I was also wondering about the effect on server-load, is this a
resource intensive process? For example is it much more efficient than
a constantly refreshing iframe?
 
P

pcx99

libsfan01 said:
Thanks for the help Laurent

I was also wondering about the effect on server-load, is this a
resource intensive process? For example is it much more efficient than
a constantly refreshing iframe?


Laurent's suggestion is a good one, but realize that as soon as you get
data back from the server the web page is going to go right back and ask
for another update. This can be quite stressful on your web server if
you don't need a continuous stream of live data.

Another approach would be instead of doing getdata() in your process
function, you setup a timeout instead....

setTimeout('getdata()',5000);

This will call getdata() but it will let 5 seconds (5,000 milliseconds)
elapse before it does it. 5,000 can be whatever you want just put in
(number of seconds * 1000).

-----------------------

There's no difference, server impact wise, between automatically
refreshing ajax calls and automatically refreshing iframes.

Hope that helps you out a bit.
 
L

Laurent Bugnion

Hi,
Laurent's suggestion is a good one, but realize that as soon as you get
data back from the server the web page is going to go right back and ask
for another update. This can be quite stressful on your web server if
you don't need a continuous stream of live data.

Another approach would be instead of doing getdata() in your process
function, you setup a timeout instead....

setTimeout('getdata()',5000);

This will call getdata() but it will let 5 seconds (5,000 milliseconds)
elapse before it does it. 5,000 can be whatever you want just put in
(number of seconds * 1000).

That's exact. I wanted to write this, and then I noticed the lack of
error handling, and then, well, I forgot. I am getting old (or should
get some sleep).
There's no difference, server impact wise, between automatically
refreshing ajax calls and automatically refreshing iframes.

That's exact too. For the web server, a request is a request is a request.

Hope that helps you out a bit.

Greetings,
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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top