SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    interval between two consecutive AJAX calls

    dear friends,
    in my js application, consecutive ajax calls are made while running in a for loop. I am using prototype.js for AJAX calls. now if I don't call any function on onComplete event then everything is ok. but if I do so, then the function I have set in an onComplete event is invoked once or twice. rest of the time it doesn't work. the code is like the follwing:

    /**
    * Moves selected options from source to destination select list.
    * @param source select list, destination select list
    * @return none
    */
    function moveMultipleOption(sourceListId, destListId)// single or multiple based on selection
    {

    var sourceListBox = document.getElementById(sourceListId);
    var destListBox = document.getElementById(destListId);

    var sourceTableName ;
    var destTableName;

    if(sourceListId == 'firstList')
    {
    sourceTableName = "left_table";
    destTableName = "right_table";
    }

    if(sourceListId == 'secondList')
    {
    sourceTableName = "right_table";
    destTableName = "left_table";
    }



    for(i = sourceListBox.options.length-1; i>=0; i--)
    {
    //for() loop is used for finding how many options are selected.
    if(sourceListBox.options[i].selected)
    {

    addOption(destListBox, sourceListBox.options[i].text, sourceListBox.options[i].value);
    sourceListBox.options[i].selected = false;

    AjaxRequest(sourceListBox.options[i].value,sourceListBox.options[i].text,sourceTableName,destTableName);
    }
    }

    }



    /**
    * Calls to server through Ajax
    * @param option value, option text, source table name and destination table name.
    * @return none
    */
    function AjaxRequest(i,textValue,sourceTableName,destTableName)
    {
    var url = '/run.php/SelectList';
    var pars = 'cmd=singleTransfer' + '&itemValue=' + i + '&textValue=' + textValue + '&sourceTableName=' + sourceTableName + '&destTableName=' + destTableName;

    var myAjax = new Ajax.Request(
    url,
    {
    method: 'get',
    parameters: pars,
    onComplete: showResponse
    }
    );
    }

    function showResponse(originalRequest)
    {
    // CODE IN THIS FUNCTION DOES NOT ALWAYS EXECUTE.
    }




    now i have solved the problem for the time being by making a single call to ajax for the whole loop. Now my question is : what is causing this problem? Can I give time delays between two consecutive requests? My hunch is that the problem lies there.


    plz reply.
    Last edited by anis_huq; May 2, 2007 at 23:12.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you have any sample snippet? This has to recall back to how you trigger your ajax call, it should have only one oncomplete callback for each call.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Are you trying to reuse the same object for all the calls or creating a new one for each call. If you reuse the same object before it completes without calling abort you will lose data.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •