[Javascript] XMLHttpRequest()

Terry Riegel riegel at clearimageonline.com
Thu Nov 15 16:52:22 CST 2007


Hello everyone. Things have been quiet for a while. I am trying to  
get some of my existing code compatible with Safari 3, and have found  
a little snag that maybe an expert would see a quick solution to.

I have a PeriodicalAjax function (listed below in its entirety) and  
it worked great on all the browsers I had tested it with until Safari  
3 came out. I discovered the line

xhr.open('post', url, true);

was not working properly and code never seemed to execute beyond it.  
So I looked it up and the third parameter basically tells JS to make  
the open a blocking open or a non-blocking open. Apparently with it  
set to true the code would execute the open request and continue   
with or without a response. So to fix things I changed it to...

xhr.open('post', url, false);

This did make it so the function and web page now works as it should,  
but since the code is stopping and waiting for the open everything  
seems sluggish and the browser become so slow it is unusable.

What I would like to do is make the subsequent code execute only when  
the open is successful. Is there an easy way to try the open then  
sleep for 10ms and then try again??

Any help will be greatly appreciated.


Thanks,


Terry Riegel




function PeriodicalAjax(url, parameters, frequency, decay, onSuccess,  
onFailure) {
  function createRequestObject() {
   var xhr;
   try {
    xhr = new XMLHttpRequest();
   }
   catch (e) {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
   }
   return xhr;
  }	
  function send() {
   if(!stopped) {
    xhr.open('post', url, false);
    xhr.setRequestHeader('Content-Type','application/x-www-form- 
urlencoded');
    xhr.onreadystatechange = function() { self.onComplete(); };
    xhr.send(parameters);
   }
  }
  this.stop = function() {
   stopped = true;
   clearTimeout(this.timer);
  }
  this.start = function() {
   stopped = false;
   this.onTimerEvent();
  }	
  this.onComplete = function() {
   if(this.stopped) return false;
   if ( xhr.readyState == 4) {
    if(xhr.status == 200) {
     if(xhr.responseText == lastResponse) {
      decay = decay * originalDecay;
     } else {
      decay = 1;
     }
     lastResponse = xhr.responseText;
     if(onSuccess instanceof Function) {
      onSuccess(xhr);
     }
     this.timer = setTimeout(function() { self.onTimerEvent(); },  
decay * frequency * 1000);
    } else {
     if(onFailure instanceof Function) {
      onFailure(xhr);
     }
    }
   }
  }
  this.getResponse = function() {
   if(xhr.responseText) {
    return xhr.responseText;
   }
  }
  this.onTimerEvent = function() {
   send();
  }
  var self = this;
  var stopped = false;
  var originalDecay = decay || 1.2;
  decay = originalDecay;
  var xhr = createRequestObject();
  var lastResponse = "";
  this.start();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20071115/0f21e955/attachment.htm>


More information about the Javascript mailing list