[Javascript] XMLHttpRequest()
Terry Riegel
riegel at clearimageonline.com
Sun Nov 18 19:49:13 CST 2007
Second post, I'll try not to be annoying, but maybe if I ask a little
differently.
I have this little snip of code...
xhr.open('post', url, false);
xhr.setRequestHeader('Content-Type','application/x-www-form-
urlencoded');
xhr.onreadystatechange = function() { self.onComplete(); };
xhr.send(parameters);
The 'false' in the open request waits until the open returns as
opposed to if I said 'true' it would work asynchronously. How would I
make it 'true' and then when it did return do the rest of the code in
the snip? (Similar to a onSuccess or onFailure trigger) I hope this
makes sense.
Any help will be appreciated.
Terry Riegel
On Nov 15, 2007, at 5:52 PM, Terry Riegel wrote:
> 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();
> }
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20071118/1d02e9a8/attachment.htm>
More information about the Javascript
mailing list