SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HELP - javascript loop reiterating too fast for ajax

    I'm struggling with a call to a php script to save data to a mysql database. Basically I've created a loop to send (n) retrieved chunks of data to be saved. Each time only the last chunk is saved. However if I insert an alert() in onreadystatechange function I'm interrupting the script and all chunks get saved. Hence I deduct the loop is reiterating too quickly. Searching forums I've seen that by using setTimeout may help but not sure how to code this.

    Can anyone help?


    function saveFlights(){
    var nb_grids = parseInt(document.getElementById('nb_grids').value);
    var direction = "out";

    for (i=1; i<=nb_grids; i++){

    var output = createUrlString(i, direction);

    if (xmlhttp){

    xmlhttp.open("GET","php_scripts/flights.php?"+output);
    xmlhttp.onreadystatechange = function(){

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
    /*do something*/

    }
    }
    }
    xmlhttp.send();
    }
    }

    function createUrlString(counter, direction){

    /*script to produce output */
    }

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you need to make a series of asynchronous requests, use the termination of the first to trigger the next and so on until there are no more to make.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. But as the number of requests is never a fixed number I can't get my head around how to do this without a loop.

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    If you use Login Ali's suggestion, and simply call the next iteration from the success callback of the previous iteration, it'll be the same as looping indefinitely. You'll probably need to keep some sort of counter of flag to store the status.

    Perhaps using an array to store the things that needs to be queried, and simply popping the array every iteration until it's empty?

  5. #5
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try this, but as I'm very new to all this it may take some time to work out your suggestion. Here goes .....

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,053
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Looks like you need to being using separate xmlhttp request objects for each iteration.
    The only code I hate more than my own is everyone else's.

  7. #7
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the moment there are none, I basically want to save the next set of data retrieved from the page to the database. For the moment if I have more than one only the last set of data is saved to the database.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,053
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Where and how is xmlhttp defined?
    The only code I hate more than my own is everyone else's.

  9. #9
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's defined at the beginning of the script:

    //Browser Support Code

    var xmlhttp = false; // The variable that makes Ajax possible!

    try{
    //If javascript version is greater than 5
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    //alert ("you are using IE");
    } catch (e){
    // If not, use the older active x object
    try{
    // If you are using IE
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    //alert ("you are using IE (2)");
    } catch (E) {
    // Else we must be using a non-IE browser
    xmlhttp = false;
    //alert ("you are using a non-IE brower");
    }
    }

    //If you are using a non-IE brower, create a javascript instace of the object
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    xmlhttp = new XMLHttpRequest();
    //alert ("You are not using Microsoft Internet Explorer");
    }

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,053
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Well… than the first thing that needs to be done is create a function to return a xmlhttp request instance, rather than having a single global instance. This is where a library such as; jQuery would come in handy considering all the cross browser compatibility stuff is being taken care of for you. All you would need to do is use the method ajax with the proper configuration options. However, since your not using jQuery or some other library that provides a normalized xmlhttp request object you will need to create your own function to return one. The reason this will need to be done is that each requests needs to be represented with a separate xmlhttp object. Therefore, *should* be a single function that returns that object taking into consideration cross browser compatibility.
    The only code I hate more than my own is everyone else's.

  11. #11
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks oddz, appreciate your help.

    jQuery just might be little too much for me at the moment. I've created a function to return XMLHttpRequest objects. However I've used the global xmlhttp instance throughout my script. I can't understand why doesn't var xmlhttp=getHTTPObject(); create my global instance but instead gives me 'xmlhttp is not defined'?

    function getHTTPObject() {
    var xhr = false;
    if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xhr;
    }

    var xmlhttp=getHTTPObject();


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
  •