[thelist] Actual Ajax question WAS: AJAX only mailing list

Brent Eades beades at almonte.com
Wed Jul 11 20:45:29 CDT 2007


Christian Heilmann wrote:

> Ajax in its most basic form means you load content via XMLhttpRequest
> in an asynchronous manner, and that is it. If someone tries to sell
> you interface tricks as Ajax that person is talking rubbish.
> 
> Ajax is a methodology, not a technology or implementation, so it is
> tricky to talk a lot about it.

I'm with you there. Which leads to a question :) How does one tell the
XMLhttpRequest object to turn the hell off, once it's retrieved the
content it was instructed to?

I'm working with some canned code, the object of which is to keep
looking for a given file, and once it's found, to plug that file into a
div on the page.

This works fine, but the problem is that the request object keeps
running even after it's found the file, sending out requests every three
seconds.

At first I thought the abort() method would help me, but it (appears) to
simply reset or restart the request -- it doesn't 'abort' it.

Here's a snippet of the code in question:

[snip]

var receiveReq = getXmlHttpRequestObject();		
updateInfo();
setInterval("updateInfo()", 3000);
function updateInfo() {

if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
    //true = asynchronous in next line
    receiveReq.open("GET", '/some/file', true);
    receiveReq.onreadystatechange = handleupdateInfo;
    receiveReq.send(null);

[snip]		

if (receiveReq.readyState == 4 && receiveReq.status == 200) {
   document.getElementById('spanResult').innerHTML =
receiveReq.responseText;

[snip]

So... there's more to the script than this obviously, but the basic
logic is if 'receiveReq.status == 404', it keeps looking, and if
'receiveReq.status == 200', the file has been found and gets displayed
in 'spanResult'.

Problem is, once the file is found, the script still keeps looking for
it every three seconds, and I can't figure out how to make it stop.
(It's obviously a waste of bandwidth to have a client page keep hitting
the server every three seconds for a file it's already retrieved.)

But I don't see how to do this. The XMLhttpRequest abort() method
doesn't -- that just seems to reset the request.

Using break, well, breaks everything...

Ideas?

-- 
Brent Eades	
Almonte, Ontario
http://almonte.com




More information about the thelist mailing list