Results 1 to 3 of 3

Thread: how to show an ajax activity indicator

  1. #1
    Join Date
    Oct 2008
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to show an ajax activity indicator

    In my application all the web pages are javascript driven when ever there is need for data from the database we are making an ajax call to the page methods in the javascript itself.

    Now my issue is like when i place this code in the server n hit any of the button lets say 'My Cart' or 'My Account' it takes some time to display the response, as it has to do the ajax call n retrieve the information from the DB.
    So, in this time delay i.e the time it takes to fetch the data from the server i have to show an ajax activity indicator how can i do this.

    Any responses ASAP is highly appreciated.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well in a typical sort of AJAX call like you describe, a function is passed some data as to the nature of the request and an id of an element (or the element itself) to place the results of the request in once it's fulfilled. The function determines the Http Request method that the browser supports and then processes it, generally with another function or some code attached in some way to the request's onreadystatechange event to populate the element on a readyState value of 4 by overwriting it's innerHTML property with the new data.

    So you have something like so:

    Code:
    function loadXmlHttp(url_or_database_query, id){
     . . .
    }
    Right at the beginning of that function, you can have some code to place a loading icon like:



    into the element that will receive the request data once it is processed.

    Now the exact nature of that code would depend upon a number of things. But one possibility would be:

    Code:
    function loadXmlHttp(url_or_database_query, id){
    var loadingarea = document.getElementById(id),
    loadingimg = document.createElement('img');
    loadingimg.src = 'loading.gif';
    while(loadingarea.lastChild) loadingarea.removeChild(loadingarea.lastChild);
    loadingarea.appendChild(loadingimg);
     . . .
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    The readyState handler usually has an assigned output element, so if the readystate equals 2, output "Requesting Data", if it equals 3 output "Retrieving Data (With any luck)".

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •