One possible goal is to make it so that a second getList() call would
not be executed until the first is completed/failed. You can set aside
a variable that holds a status. getList() can update that status
before and after the call, and also does a check on that variable
every time it's executed to determine whether it can perform another
call or not.

Something like below:

var status = 0;

function getList() {
   if (status == 0) {
        status = 1;  // talking to server
        $.ajax({
             beforeSend: function() { status = 1; },  // i'm gonna
talk to the server
             success: function() { status = 0; },  // finished talking
to server
             error: function() { status = 0; }  // finished talking to
server
        });
   }
}

// call getList();


It'll be good to set a timeout on the ajax call also so it doesn't
hold up any subsequent calls because it's taking too long for whatever
reason.

On May 15, 4:15 am, Dennis Jacobfeuerborn <djacobfeuerb...@gmail.com>
wrote:
> Hi,
> I'm trying to implement a client side cache for an ajax based
> application but I'm not sure how to deal with the asynchronous nature
> of ajax calls in jquery. What I'm basically trying is to create a
> function getList() that fetches a snippet of text from the server and
> stores it in a variable so that future invocations of getList() no
> longer have to contact the server but can immediately return the
> content of the cache-variable.
>
> The problem is that two subsequent invocations of getList() result in
> both calls contacting the server because the ajax call of the first
> invocation hasn't finished yet when the second call to getList()
> happens. At first i thought about using the "async" option but
> according to the docs this blocks the entire browser. What I'm really
> looking for is a way to only make the particular thread synchronous so
> that I can store the result before returning from the first getList()
> call.
>
> What is the proper way to handle such a situation i jquery?
>
> Regards,
>   Dennis

Reply via email to