473,386 Members | 1,753 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,386 software developers and data experts.

AJAX onreadystatechange function called too many times

I have a page that calls a javascript function every second using
setInterval():

...
<body onload="setInterval('UpdateMessages(1);', 1000 );">
...

In UpdateMessages(), I create an AJAX XMLHttpRequest object and send
the request (with open() and send()), and I make sure to set the
onreadystatechange function.

However, the onreadystatechange function is called twice the first
second the page is run, where the xmlhttp object readyState variable
is 2 and then 4, and then each of the following seconds it is called 4
times, where the readyState variable is 4, then 4, then 2, and then 4
again. What is going on? I want to be able to execute some code when
the AJAX request is complete (i.e. when the readyState is 4) but it is
then executed 3 times because I'm getting a readyState of 4 three
times! I also checked the status variable of the xmlhttp object, and
it's always 200.

If you want the code, ask. Thanks.
Aug 10 '08 #1
1 5236
bgold12 wrote:
I have a page that calls a javascript function every second using
setInterval():
Problem recognized?
... <body onload="setInterval('UpdateMessages(1);', 1000 );">
^^^^^^^^
...

In UpdateMessages(), I create an AJAX XMLHttpRequest object and send the
request (with open() and send()), and I make sure to set the
onreadystatechange function.

However, the onreadystatechange function is called [...] 4 times, where
the readyState variable is 4, then 4, then 2, and then 4 again. What is
going on?
The same method is called on different XHR objects.
I want to be able to execute some code when the AJAX request is complete
(i.e. when the readyState is 4) but it is then executed 3 times because
I'm getting a readyState of 4 three times! I also checked the status
variable of the xmlhttp object, and it's always 200.
Do not use setInterval() with repeated XHR. Call UpdateMessages(), which
should be updateMessages(), directly, and in the listener call
window.setTimeout() when readyState == 4. Be sure to make an asynchronous
request so that the UA is not blocked in the process. You should be reusing
the created XHR object.

This has been asked here before.
If you want the code, ask. Thanks.
The usual way here is: If you want help, post the minimum of the relevant
code. One call, unless it is to invoke a known method eventually, certainly
does not qualify as such.

<http://jibbering.com/faq/#FAQ2_3>
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>
Aug 10 '08 #2

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

Similar topics

1
by: vachacz | last post by:
hi i'm struggling with integration of a js class aimed for ajax handling with fireing actions from the class. here's my code sample: function AjaxLibLoad(httpActionParam, targetLayerParam){...
2
by: Mark Knochen | last post by:
Hi, i have a little problem with ajax. The follow functions are in my site: function sndReq(ID,divID) { resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
5
by: Heofz | last post by:
Hey all, I've been banging my head against a brick wall on this one for the last 12 hours, and the time has come for me to give up and consult the gurus on this one. The below URL contains a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.