473,490 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Dec 18 '06 #1
4 2699
Hi,

libsfan01 wrote:
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 18 '06 #2
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?

>
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 18 '06 #3
libsfan01 wrote:
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?

>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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

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.

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Dec 18 '06 #4
Hi,

pcx99 wrote:
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
372
by: William | last post by:
function saveText( scroll_list, t_area, listToBeUpdated ) { var updated = new Option(); updated.value = t_area.value; updated.text = t_area.text; for(var i = 0; i < scroll_list.options.length;...
1
9469
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
4
6905
by: libsfan01 | last post by:
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...
2
3818
by: dmagliola | last post by:
Hello all, I'm experiencing a problem with ASP.Net for which I can't find a reasonable explanation, or any information. I'm currently developing an application that, through AJAX, asks the...
1
25073
by: Charlie | last post by:
I am trying to make an XMLHttpRequest which violates the default "same- origin"policy in Firefox. I checked the archives and found a method that should work but it does not. Below is the test code...
1
5285
by: Tarik Monem | last post by:
OK, I'm pretty sure this cannot work because I'm trying to use JavaScript (client-side) to write to an xml file (which is server-side) using XMLHttpRequest. Can I use PHP do what I'm trying to do?...
0
2849
by: das | last post by:
Hello all, I am using the SqlDependency to subscribe to any new inserts into a database table, I enabled the DB to be borker ready and subscrbed to Query notifications on the database. My C#...
2
2431
by: gradinafrica | last post by:
I'm trying to create a log out button that uses AJAX to call a php file which ends the current session: //logout.php <?php if (!session_start()); session_destroy(); //Destroys the...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.