AJAX onreadystatechange function called too many times

Discussion in 'Javascript' started by bgold12, Aug 10, 2008.

  1. bgold12

    bgold12 Guest

    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.
    bgold12, Aug 10, 2008
    #1
    1. Advertising

  2. 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>
    Thomas 'PointedEars' Lahn, Aug 10, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. djskrill
    Replies:
    9
    Views:
    698
    djskrill
    Oct 1, 2003
  2. Apricot
    Replies:
    4
    Views:
    524
    velthuijsen
    Apr 16, 2004
  3. Radu
    Replies:
    7
    Views:
    708
    charlie_Stell
    Aug 28, 2009
  4. Pete Coolidge

    OnItemCreated and OnItemDataBound called too many times?

    Pete Coolidge, Jul 4, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    241
    Pete Coolidge
    Jul 4, 2003
  5. Replies:
    2
    Views:
    190
    Benjamin
    Sep 14, 2006
Loading...

Share This Page