Simple Ajax

B

Bryan A

Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.

Heres my code:

var xmlhttp = false ;

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try {
xmlhttp = new XMLHttpRequest ();
}
catch (e) {
xmlhttp = false}
}
function myXMLHttpRequest ()
{
var xmlhttplocal;
try {
xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
catch (e) {
try {
xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
catch (E) {
xmlhttplocal = false;
}
}

if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
try {
var xmlhttplocal = new XMLHttpRequest ();
}
catch (e) {
var xmlhttplocal = false;
}
}
return (xmlhttplocal);
}
var mnmxmlhttp = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;

var i=0;
var ii = 0;
function ajax_update()
{

url = "http://www.example.com/";
target2 = document.getElementById ('update');

ii = i++;

var content = "i=" + ii ;

mnmxmlhttp = new myXMLHttpRequest ();
if (mnmxmlhttp) {
mnmxmlhttp.open ("POST", url, true);
mnmxmlhttp.setRequestHeader ('Content-Type',
'application/x-www-form-
urlencoded');
mnmxmlhttp.send (content);
errormatch = new RegExp ("^ERROR:");

target2 = document.getElementById ('update');

mnmxmlhttp.onreadystatechange = function () {
if (mnmxmlhttp.readyState == 4) {
mnmString =
mnmxmlhttp.responseText;

if (mnmString.match
(errormatch)) {
mnmString =
mnmString.substring (6, mnmString.length);

target =
document.getElementById ('update');
target2.innerHTML =
mnmString;

} else {
target =
document.getElementById ('update');
target2.innerHTML =
mnmString;

}
}
}
}


setTimeout('ajax_update()',2000);
}


Thanks for any help
-Bryan
 
D

Diego La Monica

Hi Bryan,
the fastest way is as I will explain in the following thoughts:

1. setTimeout must be changed to setInterval to make sure that each 2
second the ajax_update() method to start.
2. in ajax_update method you have to check if you're already in the
requesting mode (eg. global variables that store the status)
3. in the same ajax_update method keep the count of times you call it
and when you're reached 50 times you've reached the 100 seconds
(2second x 50 times). If you reached the 50 times call the abort
method of xmlhttprequest still active and notify the problem.
4. If the request end sucessfully, reset the counter and the status
variable (you must notify that the xmlhttprequest object is not still
busy).

Hope it will help you!

Diego La Monica
http://jastegg.it/jastASlide/
http://jastegg.it/
 
T

Thomas 'PointedEars' Lahn

Bryan said:
Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.

You need to call
[...] setTimeout('ajax_update()',2000);

in the event listener, too. You should make that window.setTimeout(...)
and you may want to pass a Function object reference instead.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Diego said:
1. setTimeout must be changed to setInterval to make sure that each 2
second the ajax_update() method to start.

Nonsense. This is *asynchronous* request-response handling. With using
window.setInterval() you risk messing with a request-response in progress.


PointedEars
 
D

Diego La Monica

Nonsense.  This is *asynchronous* request-response handling.  With using
window.setInterval() you risk messing with a request-response in progress..

Thomas, you've stopped to read my message only on the 1st point, well,
read the next three points (for your simplicity I'll post again them
below, please read all first and then evaluate youre response):

1. setTimeout must be changed to setInterval to make sure that each 2
second the ajax_update() method to start.
2. in ajax_update method you have to check if you're already in the
requesting mode (eg. global variables that store the status)
3. in the same ajax_update method keep the count of times you call it
and when you're reached 50 times you've reached the 100 seconds
(2second x 50 times). If you reached the 50 times call the abort
method of xmlhttprequest still active and notify the problem.
4. If the request end sucessfully, reset the counter and the status
variable (you must notify that the xmlhttprequest object is not still
busy).


For your information I know that ajax is asynchronous else its name, I
think, would be SJAX. Don't you?

Diego La Monica
 
D

Diego La Monica

Bryan said:
Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.

You need to call
[...]         setTimeout('ajax_update()',2000);

Thomas, did you ever done a performance test between setTimeout and
setInterval?
So I suggest you to take a look to the CPU.

Diego La Monica
 
T

Thomas 'PointedEars' Lahn

Diego said:
Thomas, you've stopped to read my message only on the 1st point, well,

For good reason. Your approach is wrong from the beginning.
read the next three points (for your simplicity I'll post again them
below, please read all first and then evaluate youre response):

1. setTimeout must be changed to setInterval to make sure that each 2
second the ajax_update() method to start.

No, it MUST NOT. (sic!)
2. in ajax_update method you have to check if you're already in the
requesting mode (eg. global variables that store the status)

Nonsense. When xhr.readyState == 4 it is time to plan making a new request
in the event listener; not before. Else you *might* have more current
display, but you would hammer the server. And you would buy this seemingly
being more up-to-date with the client consuming much more heap than
necessary. Because an increasing number of XHR objects would need to be
created in the process since you can *not* reuse an XHR object when its
request-response handling is in progress.
3. in the same ajax_update method keep the count of times you call it
and when you're reached 50 times you've reached the 100 seconds
(2second x 50 times). If you reached the 50 times call the abort
method of xmlhttprequest still active and notify the problem.

Utter nonsense. (You would even use a tight loop to count milliseconds, yes?)
4. If the request end sucessfully, reset the counter and the status
variable (you must notify that the xmlhttprequest object is not still
busy).

Completely unnecessary, and error-prone. Instead, after the send() method
would have been called, another window.setTimeout() call has to take place
that plans calling the abort() method of the XHR object in question the
specified time after the send() call.

var me = arguments.callee;
xhr.open(...);
...
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
...
window.setTimeout(me, 2000);
}
};
xhr.send();
window.setTimeout(function() { xhr.abort(); }, 100000);
For your information I know that ajax is asynchronous else its name, I
think, would be SJAX. Don't you?

You don't even appear to know that "AJAX" is a misnomer, let alone how XHR
works.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Diego said:
Thomas said:
Bryan said:
Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.
You need to call
[...] setTimeout('ajax_update()',2000);

Thomas, did you ever done a performance test between setTimeout and
setInterval?

Yes, I did. I have empirical data from production code to show that letting
code that is planned to be evaluated/called with window.setTimeout() call
window.setTimeout() again makes up to about 50% less CPU load than using
window.setInterval(), especially with short intervals, because with
window.setInterval() planned tasks are very likely to accumulate and/or
overlap. The price for more calls that are required then is negligibly
small in comparison.
So I suggest you to take a look to the CPU.

I was about to recommend that to you. In fact, we have discussed this before.


PointedEars
 
D

Diego La Monica

For good reason.  Your approach is wrong from the beginning.

I'm not agree, but are the two differents point of view. And I don't
think your is better so go on and take a while to read the message
entirelly before say "you're wrong!". It's more educated.
No, it MUST NOT. (sic!)

Damn, Thomas, your obfuscated from your idea... or I'm explaining me
incorrectly.
Well! Let's try to explain better:

The requirements are:
- each 2 seconds to check for updates. (setInterval starts the
function foo() each 2 seconds)
- after 100 seconds if no response will come then notify to the user
with something other.

Follows javascript (maybe you understand better it than my words):

var ImInside = false;
var loops = 0;
var maxLoops = 50;

function ajax_update(){
// Are the loop to the limit
if(loop++==maxLoops){
// notify something goes wrong then...
if(xhr.readyState!=4) xhr.abort();
loops = 0;
imInside = false;
return;
}
if ImInside return;
ImInside = true;

xhr.open(...)
xhr.oneadystatechange = function(){
if(xhr.readyState==4){
// Do all updates then ...
loops = 0;
ImInside = false;
}
}

}

var i = setInterval( ajax_update , 2000);



Nonsense.  When xhr.readyState == 4 it is time to plan making a newrequest
in the event listener; not before.  Else you *might* have more current
display, but you would hammer the server.  And you would buy this seemingly
being more up-to-date with the client consuming much more heap than
necessary.  Because an increasing number of XHR objects would need to be
created in the process since you can *not* reuse an XHR object when its
request-response handling is in progress.

What you wrote confirm that you didn't understand... sorry that I
didn't explain correctly what I means.
Listen the above script. You will see that *only one xhr at the time
is available* and *only one client/server communication* is active. I
don't create excessive traffic between client and server. And I will
inform the user only if error happens ( your code is wrong: xhr.abort
() need to be called only if after 100 seconds the server hang ).
Utter nonsense.  (You would even use a tight loop to count milliseconds, yes?)


Completely unnecessary, and error-prone.  Instead, after the send() method
would have been called, another window.setTimeout() call has to take place
that plans calling the abort() method of the XHR object in question the
specified time after the send() call.

Set Timeout has a great defeat: eats cpu resources as chips.
  var me = arguments.callee;
  xhr.open(...);
  ...
  xhr.onreadystatechange = function()
  {
    if (xhr.readyState == 4)
    {
      ...
      window.setTimeout(me, 2000);
    }
  };
  xhr.send();
  window.setTimeout(function() { xhr.abort(); }, 100000);


You don't even appear to know that "AJAX" is a misnomer, let alone how XHR
works.

Why you'd like to bring that pacific conversation to a flame?

Regards.

Diego La Monica
 
T

Thomas 'PointedEars' Lahn

Diego said:
I'm not agree, but are the two differents point of view. And I don't
think your is better so go on and take a while to read the message
entirelly before say "you're wrong!". It's more educated.

So you really think letting the client eat up more of the scarce resource,
local heap memory, every two seconds, is the actually right thing to do?
Damn, Thomas, your obfuscated from your idea...

I'm not. You are missing the point instead.
[...]
Follows javascript (maybe you understand better it than my words):

var ImInside = false;
var loops = 0;
var maxLoops = 50;

function ajax_update(){
// Are the loop to the limit
if(loop++==maxLoops){
// notify something goes wrong then...
if(xhr.readyState!=4) xhr.abort();
^^^
So where does this come from? Think about it.
loops = 0;
imInside = false;
return;
}
if ImInside return;
ImInside = true;

xhr.open(...)
xhr.oneadystatechange = function(){
if(xhr.readyState==4){
// Do all updates then ...
loops = 0;
ImInside = false;
}
}
}

var i = setInterval( ajax_update , 2000);

You miss the point. In order to be able to do that you need to create a new
XHR object every time you call ajax_update() because you can not reliably
reuse the existing object.
What you wrote confirm that you didn't understand...

It's you who doesn't understand.
sorry that I didn't explain correctly what I means.
Listen the above script. You will see that *only one xhr at the time
is available* and *only one client/server communication* is active.

You are mistaken. Again: The current XHR object can *not be reused reliably
while the request-response handling is in progress*, so you have to work
with a *new one*. Each time, in this case every two seconds.
I don't create excessive traffic between client and server.

Yes, you do. Depends on your definition of excessive, of course.
And I will inform the user only if error happens ( your code is wrong:

It isn't. It certainly can be perfected, but it wasn't supposed to be a
complete solution to begin with.
xhr.abort () need to be called only if after 100 seconds the server hang ).

If the server hanged then abort() will cancel the request. If it did not,
calling abort() should do no harm; however, it is possible to consider the
current value of `readyState' before calling abort(), and to catch
exceptions in the worst case.
Set Timeout has a great defeat: eats cpu resources as chips.

You are mistaken. This virtue belongs to window.setInterval(), and in this
case the local heap memory is also affected negatively a great deal by the
latter approach.
Why you'd like to bring that pacific conversation to a flame?

I am simply stating the facts, and I have to add: you also don't know how
window.setInterval() works, else you would not recommend using it in this
time-critical scenario.


PointedEars
 
J

Jorge

Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.

function newXHR () {
var xhr;
if (XMLHttpRequest) {
return new XMLHttpRequest();
} else {
try {
xhr= new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr= new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xhr= false;
}
}
}
return xhr;
}

function getTime () {
return (new Date()).getTime();
}

var i= 0;
var url= "http://www.example.com/";
var errorMatch= new RegExp("^ERROR:");
var target= document.getElementById('update');

function ajax_update () {
var xhr, str, time= getTime();

if (xhr= newXHR()) {
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xhr.onreadystatechange= function () {
if (xhr.readyState > 3) {
if (xhr.status !== 200) {
alert("XHR: Oops !");
} else {
str= xhr.responseText;
if (str.match(errorMatch)) {
str= str.substring(6, str.length);
}
target.innerHTML= str;
time= ((time= getTime()- time) > 2000) ? 0 : (2000- time);
setTimeout(ajax_update, time);
}
}
};
xhr.send("i=" + (++i));
}
}

ajax_update();

HTH,
 
J

Jorge

Bryan said:
Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.

function newXHR () {
var xhr;
if (XMLHttpRequest) {
return new XMLHttpRequest();
} else {
try {
xhr= new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr= new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xhr= false;
}
}
}
return xhr;
}

function timer (p) {
return (new Date()).getTime()- p;
}

var i= 0;
var url= "http://www.example.com/";
var errorMatch= new RegExp("^ERROR:");
var target= document.getElementById('update');

function ajax_update () {
var xhr, str, time= timer(0);

if (xhr= newXHR()) {
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xhr.onreadystatechange= function () {
if (xhr.readyState > 3) {
if (xhr.status !== 200) {
alert("XHR: Oops !");
} else {
str= xhr.responseText;
if (str.match(errorMatch)) {
str= str.substring(6, str.length);
}
target.innerHTML= str;
time= ((time= timer(time)) > 2000) ? 0 : (2000- time);
setTimeout(ajax_update, time);
}
}
};
xhr.send("i=" + (++i));
}
}

ajax_update();

HTH,
 
J

Jorge

Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.

function newXHR () {
var xhr;
if (XMLHttpRequest) {
return new XMLHttpRequest();
} else {
try {
xhr= new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr= new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xhr= false;
}
}
}
return xhr;
}

function timer (p) {
return (new Date()).getTime()- p;
}

var i= 0;
var url= "http://www.example.com/";
var errorMatch= new RegExp("^ERROR:");
var target= document.getElementById('update');

function ajax_update () {
var xhr, str, time= timer(0);

if (xhr= newXHR()) {
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xhr.onreadystatechange= function () {
if (xhr.readyState > 3) {
if (xhr.status !== 200) {
alert("XHR: Oops !");
} else {
str= xhr.responseText;
if (str.match(errorMatch)) {
str= str.substring(6, str.length);
}
target.innerHTML= str;
time= ((time= timer(time)) > 2000) ? 0 : (2000- time);
setTimeout(ajax_update, time);
}
}
};
xhr.send("i=" + (++i));
}
}

ajax_update();

HTH,
 
J

Jorge

Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.

function newXHR () {
var xhr;
if (XMLHttpRequest) {
return new XMLHttpRequest();
} else {
try {
xhr= new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr= new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xhr= false;
}
}
}
return xhr;
}

function timer (p) {
return (new Date()).getTime()- p;
}

var i= 0;
var url= "http://www.example.com/";
var errorMatch= new RegExp("^ERROR:");
var target= document.getElementById('update');

function ajax_update () {
var xhr, str, time= timer(0);

if (xhr= newXHR()) {
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
xhr.onreadystatechange= function () {
if (xhr.readyState > 3) {
if (xhr.status !== 200) {
alert("XHR: Oops !");
} else {
str= xhr.responseText;
if (str.match(errorMatch)) {
str= str.substring(6, str.length);
}
target.innerHTML= str;
time= ((time= timer(time)) > 2000) ? 0 : (2000- time);
setTimeout(ajax_update, time);
}
}
};
xhr.send("i=" + (++i));
}
}

ajax_update();

HTH,
 
D

Diego La Monica

Diego:

Thomas:
So you really think letting the client eat up more of the scarce resource,
local heap memory, every two seconds, is the actually right thing to do?

Diego:
Why are you thinking that my idea could eat so much resource?

Thomas:
Damn, Thomas, your obfuscated from your idea...

I'm not.  You are missing the point instead.
[...cut...]
You miss the point.  In order to be able to do that you need to create a new
XHR object every time you call ajax_update() because you can not reliably
reuse the existing object.

Diego:
That demonstrate that you're obfuscated from your interpretation of
what I wrote without reading what I've really wrote!
Gift me 20 seconds of yours and try the example that I've created to
demonstrate you what I was saying since yesterday:
http://jastegg.it/tests/javascript/xhr2sec.htm
Take care: use Firebug to ensure that you can see how many client/
server connection will be active.
It's you who doesn't understand.

Look at the above example then you rethink! ;)
You are mistaken.  Again: The current XHR object can *not be reused reliably
while the request-response handling is in progress*, so you have to work
with a *new one*.  Each time, in this case every two seconds.


Yes, you do.  Depends on your definition of excessive, of course.

is *one* client/server communication per time for you *too much*? Well
explain me how you do it without it! :)
It isn't.  It certainly can be perfected, but it wasn't supposed to be a
complete solution to begin with.

Look at the example!
If the server hanged then abort() will cancel the request.  If it did not,
calling abort() should do no harm; however, it is possible to consider the
current value of `readyState' before calling abort(), and to catch
exceptions in the worst case.

Look at the example.
You are mistaken.  This virtue belongs to window.setInterval(), and in this
case the local heap memory is also affected negatively a great deal by the
latter approach.



I am simply stating the facts, and I have to add: you also don't know how
window.setInterval() works, else you would not recommend using it in this
time-critical scenario.

I've used it in the examle but... well let's think that I don't know
window.setInterval method what's wrong are you bringing reasons?

Cheers.
Diego La Monica
 
D

Diego La Monica

Diego said:
Thomas said:
Bryan A wrote:
Is there a way to add a timeout to this script so that it times out at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.
You need to call
[...]         setTimeout('ajax_update()',2000);
Thomas, did you ever done a performance test between setTimeout and
setInterval?

Yes, I did.  I have empirical data from production code to show that letting
code that is planned to be evaluated/called with window.setTimeout() call
window.setTimeout() again makes up to about 50% less CPU load than using
window.setInterval(), especially with short intervals, because with
window.setInterval() planned tasks are very likely to accumulate and/or
overlap.  The price for more calls that are required then is negligibly
small in comparison.

Please, let us see your test cases.
I was about to recommend that to you.  In fact, we have discussed this before.

I did... and results let me puzzled. Where can I see your tests?

Thanks!
 
T

Thomas 'PointedEars' Lahn

Diego said:
Diego:
Why are you thinking that my idea could eat so much resource?

This is Usenet, not online chat. However, I thought it would need to do
that because it was not obvious to me that you don't call open() when the
request is in progress.
[...]
That demonstrate that you're obfuscated from your interpretation of
what I wrote without reading what I've really wrote!
Gift me 20 seconds of yours and try the example that I've created to
demonstrate you what I was saying since yesterday:
http://jastegg.it/tests/javascript/xhr2sec.htm
Take care: use Firebug to ensure that you can see how many client/
server connection will be active.

Without looking at the test case: OK, so there's `ImInside'. Blame me for
overlooking syntactically wrong and badly written code.

But try to answer this: In what way would calling ajax_update() repeatedly
with window.setInterval(), using global variables, and observing a
precondition be better than calling ajax_update() once, calling it again
only when the connection was possible and the request-response handling was
complete, not using any additional global variables, and canceling the
request after a well-defined amount of time that is visible at a glance?
I've used it in the examle but... well let's think that I don't know
window.setInterval method what's wrong are you bringing reasons?

Parse error. But if I were to make an educated guess, I think I have
answered that question already in <among other postings.


PointedEars
 
D

Diego La Monica

Thomas:
Without looking at the test case: OK, so there's `ImInside'.  Blame me for
overlooking syntactically wrong and badly written code.

Diego:
Acc... I made a mistake! Sorry...

Thomas:
But try to answer this: In what way would calling ajax_update() repeatedly
with window.setInterval(), using global variables, and observing a
precondition be better than calling ajax_update() once, calling it again
only when the connection was possible and the request-response handling was
complete, not using any additional global variables, and canceling the
request after a well-defined amount of time that is visible at a glance?

Diego:
If you take a look the original Bryan's request is:
- poll the server each 2 seconds
- if something goes wrong abort the request after 100 seconds.

With setInterval you poll the server every 2 seconds (0, 2, 4, 6 etc.
etc.).
With setTimeout you poll the server every 2 seconds + server response
lag (0, 2+[time to response 1], 4+[time to response 2], and so on).

The abort should be called only if something goes wrong not every 100
seconds else you risk to abort a request started just few seconds
before.

I think that what I wrote is clear, please tell me if something is not
so.

Cheers
Diego La Monica
 
T

Thomas 'PointedEars' Lahn

Diego said:
Thomas:

Diego:
Acc... I made a mistake! Sorry...

I thought I already asked you to stop trying to make this a chat. Learn to
quote before you post to Usenet. Examples of proper quoting are all over
this group, if you cared to notice.

[Quoting fixed]
If you take a look the original Bryan's request is:
- poll the server each 2 seconds
- if something goes wrong abort the request after 100 seconds.

With setInterval you poll the server every 2 seconds (0, 2, 4, 6 etc.
etc.).

That is definitely _not_ what happens. Have you not even understood your
own code?
With setTimeout you poll the server every 2 seconds + server response
lag (0, 2+[time to response 1], 4+[time to response 2], and so on).

Nonsense. The next request is made, through calling the called method
again, 2 seconds after the previous one was finished (successfully or
otherwise), not before. And that is good so.
The abort should be called only if something goes wrong not every 100
seconds

That is in your imagination, though, not in the OP's specification.
else you risk to abort a request started just few seconds before.

Yes, a condition needs to be implemented in the function used in the
window.setTimeout() call after send(), to prevent that.

However, the OP's specification says to cancel the request after that time
*no matter what*. And even if we extend the original specification like you
interpreted it, it still does not call for using window.setInterval() with
that rather bogus approach of yours.


PointedEars
 
D

Diego La Monica

With setInterval you poll the server every 2 seconds (0, 2, 4, 6 etc.
That is definitely _not_ what happens.  Have you not even understood your
own code?

Ok I've served you as is... I try to explain better my code: the poll
try to start (if any other request is stil active) every 2 seconds.

I cite the Brian's question:
<cite>
Is there a way to add a timeout to this script so that it times out
at
a certain time. So it would be auto updating every 2seconds and it
would timeout like after 100 seconds with a message?.
</cite>

"...auto updating every 2 seconds..."

My interpretation is that every 2 seconds should start the ajax_update
method, your interpretation is that after 2 second since the last
execution ajax_update method should start. Well are 2 Point of view
differents! I'm not stating you're wrong... You, instead, do it.
With setTimeout you poll the server every 2 seconds + server response
lag (0, 2+[time to response 1], 4+[time to response 2], and so on).

Nonsense.  The next request is made, through calling the called method
again, 2 seconds after the previous one was finished (successfully or
otherwise), not before.  And that is good so.

I don't think is good so.
Test case with your consideration:
in the following: "request" is the times that xhr.open is called,
"response" is the the readyState=4 of xhr object.

- 1st client request: 0,00 (start point)
- 1st server response: 1,87 seconds (then wait 2,00 seconds)
- 2nd client request 3,87 seconds (result of 1,87 + 2,00 of timeout)
- 2nd server response: 1,99 seconds (then wait 2,00 seconds)
- 3rd client request: 7,86 seconds (result of 3,87 +1,99 +2,00
seconds)
- 3rd server response: 3,00 seconds (then wait 2,00 seconds)
- 4th client request: 12,86 seconds (result of 7,86 +3,00 +2,00
seconds)
.... and so on!

Test case with my alghoritm:

- 1st client request: 2,00 (start point)
- 1st server response: 1,87 seconds
- 2nd client request: 4,00
- 2nd server response: 1,99 seconds
- 3rd client request: 6,00
- 3rd server response: 3,00 seconds
- 4th client request: 10,00
.... and so on!

Let decide bryan which is what he want... or Bryan and you are the
same person?
That is in your imagination, though, not in the OP's specification.

bryan wrote: "...auto updating every 2seconds and it would timeout
like after 100 seconds..."

It's in the Bryan request not in my imagination.
Yes, a condition needs to be implemented in the function used in the
window.setTimeout() call after send(), to prevent that.

However, the OP's specification says to cancel the request after that time
*no matter what*.  

Sure? Bryan wrote: "...it would timeout like after 100 seconds with a
message?. "
So matter what. Or did I read it wrong?
And even if we extend the original specification like you
interpreted it, it still does not call for using window.setInterval() with
that rather bogus approach of yours.

mmm... Yor are going again to make this post a flame. Please, be
professional as I think you are and weight one's word. If you proceed
again on this way I will stop to discuss with you!

Regards!
Diego La Monica
 

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
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top