473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Simple Ajax

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
Nov 17 '08 #1
20 2184
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/

On 17 Nov, 06:59, Bryan A <Bryan.Andr...@gmail.comwrote:
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
Nov 17 '08 #2
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);
in the event listener, too. You should make that window.setTimeout(...)
and you may want to pass a Function object reference instead.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 17 '08 #3
Diego La Monica wrote:
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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 17 '08 #4
On Nov 17, 7:46*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Diego La Monica wrote:
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..
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
PointedEars
--
* * realism: * *HTML 4.01 Strict
* * evangelism: XHTML 1.0 Strict
* * madness: * *XHTML 1.1 as application/xhtml+xml
* * * * * * * * * * * * * * * * * * * * * * * * * * -- Bjoern Hoehrmann
Nov 17 '08 #5
On Nov 17, 7:45*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
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?
So I suggest you to take a look to the CPU.

Diego La Monica
Nov 17 '08 #6
Diego La Monica wrote:
Thomas 'PointedEars' Lahn wrote:
>Diego La Monica wrote:
>>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.

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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Nov 17 '08 #7
Diego La Monica wrote:
Thomas 'PointedEars' Lahn wrote:
>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.
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 17 '08 #8
On 17 Nov, 21:21, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Diego La Monica wrote:
Thomas 'PointedEars' Lahn wrote:
Diego La Monica wrote:
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. *Withusing
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,

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.
>
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!)
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);

>
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 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 ).
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.
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);
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.
Why you'd like to bring that pacific conversation to a flame?

Regards.

Diego La Monica
Nov 17 '08 #9
On Nov 17, 6:59*am, Bryan A <Bryan.Andr...@gmail.comwrote:
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,
--
r2d3
Nov 17 '08 #10
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?.
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,
--
r2d3
Nov 17 '08 #11
On Nov 17, 6:59*am, Bryan A <Bryan.Andr...@gmail.comwrote:
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,
--
r2d3
Nov 17 '08 #12
On Nov 17, 6:59*am, Bryan A <Bryan.Andr...@gmail.comwrote:
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,
--
r2d3
Nov 17 '08 #13
On Nov 17, 9:31*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Diego La Monica wrote:
Thomas 'PointedEars' Lahn wrote:
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.
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.
I did... and results let me puzzled. Where can I see your tests?

Thanks!
Nov 18 '08 #14
Diego La Monica wrote:
Thomas 'PointedEars' Lahn wrote:
>>>For good reason. Your approach is wrong from the beginning.

Diego:
>>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.

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?
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?
>>>>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.
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.

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 <news:49**************@PointedEars.de>,
among other postings.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Nov 19 '08 #15
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
Nov 19 '08 #16
Diego La Monica wrote:
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...
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]
>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?

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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Nov 19 '08 #17
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?
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?
>
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.
bryan wrote: "...auto updating every 2seconds and it would timeout
like after 100 seconds..."

It's in the Bryan request not in my imagination.
>
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*. *
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
Nov 19 '08 #18
On Nov 19, 9:26*pm, Diego La Monica <diego.lamon...@gmail.comwrote:
>
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).
No Diego, not necessarily.
>
- 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!
You can do that as well with a setTimeout.
In general it's usually better to use a self-re-setting setTimeout
than a setInterval.

--
Jorge.
Nov 19 '08 #19
On Nov 19, 9:49*pm, Jorge <jo...@jorgechamorro.comwrote:
On Nov 19, 9:26*pm, Diego La Monica <diego.lamon...@gmail.comwrote:
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).

No Diego, not necessarily.
Why not? If you see the Thomas's alghoritm behavior is as described
(above).
>
- 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!

You can do that as well with a setTimeout.
Agree.
In general it's usually better to use a self-re-setting setTimeout
than a setInterval.
Jorge, I understand clear like the water what you write, maybe that
Thomas would say me the same, maybe. I will document about it, but if
you have some links that explain accuratelly why, I'll apreciate them
a lot!

Cheers!
Diego La Monica
Nov 19 '08 #20
On 19 Nov, 22:37, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Diego La Monica wrote:
Thomas 'PointedEars' Lahn wrote:
Diego La Monica wrote:
Thomas, did you ever done a performance test between setTimeout and
setInterval?
Yes, I did. *I have empirical data from production code to show thatletting
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.

As I said, it was/is production code where I observed that difference, [....]
I am sure you can build a test case for yourself based on this, although
common sense should suffice.
I don't like to recreate the wheel.. You told me that your code with
setTimeout is better in your production environment, but that not
means that is always better than setInterval.
Because I like to learn always more than I know, I will post you 2
links: 1 article and 1 example.

The 1st link[1] describe the difference between setTimeout and
setInterval. As I can read there, setInterval "is a function that can
execute other javascript statement EVERY x interval. The interval unit
is millisecond."
Bryan asked us to do something every 2 seconds, not AFTER 2 seconds.
But leave it.
The 2nd link[2] will show you an example of using setInterval against
setTimeout. Take a look to the results.
I'm considering those information quite reliable. Don't you?

With regards.
Diego La Monica

[1] http://rapd.wordpress.com/2007/10/11...s-setinterval/
[2] http://dhtmlkitchen.com/learn/js/per...tInterval.html

Nov 20 '08 #21

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

Similar topics

2
by: Alex | last post by:
Example uploaded to: http://www.clickatus.com/ajax/ BTW - This is for FIREFOX, won't work in IE. I don't know why but when it is executed the browser still in loading state... Even though...
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
1
by: eddie | last post by:
I have a form. The form submits correctly and the php script GET's the ?variables=whatever&variables2=whatever2, but when I try and use AJAX, I receive no errors, my innerHTML is changed with the...
1
by: Boris Twila | last post by:
I just want to have 1 drop down list fill the other drop down list without a round trip. Is there some really simple ajax thing i can copy and use, or is it a big deal do i habe to install an...
0
by: minnie | last post by:
An AJAX Simple Example for PHP Article from http://www.joyistar.com Introduction: AJAX WebShop 3 Beta2 supports PHP programming by integrating PHP5 development environment. Here we will give an...
2
by: brakai295 | last post by:
Hi there, I need someone to customise an autocompletion AJAX script a little bit. This free script here (demo | info & download) offers you auto-completion when typing in a country's name. I...
0
by: darrel | last post by:
I was playing with AJAX a bit last year but never really took the time to fully analyze the various AJAX libraries out there for ASP.net 1.1 We're finally using 2.0, so have begun playing with...
3
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I'm trying to follow the instructions on http://www.asp.net/learn/videos/view.aspx?tabid=63&id=75 for a simple AJAX demo. However, my code updates all 3 labels even though only 1 of them is...
1
by: msg2ajay | last post by:
hello, i am very new to Ajax i have tried an example but it showing some error can anybady tell me where i am doing mistake... my coding is as follows ajax.html: ----------- ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.