Topic: drop response if from earlier request

Hey there,

i'm currently trying to implement an autosuggest solution and running into some asynchronous call problems.

In my script i type into a text field and every keystroke invokes an xajax call, doing a mysql query. But the time to get a response from mysql varies greatly, so it might happen, that the result from an earlier request is sent back to the client later than the most recent request.
So my script might display the older response because it was sent back later thant the response from the newest request.

If i would use synchronous calls, which I tried, the user always has to wait for the response, before he can go on typing another letter into the form input field.

What would be a good solution for this? Can anyone help me?

Re: drop response if from earlier request

I'm basically trying to solve the same problem as the original poster: I want to be able to drop "old/outdated" xajax requests/responses, if circumstances have changed on the client-side, that no longer need the originally requested data.

In my example I simply have a table that updates dynamically using something like this:

setInterval('xajax_UpdateList(g_date)', 10000);

However, if the user made changes to the g_date variable (requesting a different date), I want to drop any updates coming in for the previous date. Is there a mechanism in place to achieve that?

Re: drop response if from earlier request

I got this to work by using code I found here on the forum:

Array.prototype.removeItems = function(itemsToRemove)
{
    if (!/Array/.test(itemsToRemove.constructor))
        itemsToRemove = [ itemsToRemove ];

    var j;
    for (var i = 0; i < itemsToRemove.length; i++)
    {
        j = 0;
        while (j < this.length)
        {
            if (this[j] == itemsToRemove[i])
            {
                this.splice(j, 1);
            }
            else
            {
                j++;
            }
        }
    }
}

var openRequests = new Array();

function IsXajaxProcessingRequest()
{
    return openRequests.length != 0;
}

function AbortXajaxRequests()
{
    for (var i = openRequests.length-1; i >= 0; i--)
    {
        var request = openRequests[i];
        xajax.abortRequest(request);
    }
}

function MonitorXajaxRequests()
{
    xajax.callback.global.onRequest = function(oRequest)
    {
        openRequests.push(oRequest);
    }

    xajax.callback.global.onComplete = function(oRequest)
    {
        openRequests.removeItems(oRequest);
    }
}

Call MonitorXajaxRequests() once when the page loads, for instance like this:

onload="setInterval('if (!IsXajaxProcessingRequest()) xajax_UpdateList(g_page, g_tellerId, g_date)', 10000); MonitorXajaxRequests();"

Call AbortXajaxRequests() when ever you need to cancel pending xajax requests.

Of course one should also not forget to pause/stop the setInterval() function, which I do in my case by checking IsXajaxProcessingRequest().