tags:

views:

18

answers:

1

Welcome,

I have question.

On my website i have form what allow user to submit data via POST (ajax). The problem is, when user submit field, ajax start request... if server is free, or user network is fast everything work very smooth. User can send messages very fast, and everything working.

But if server is overloaded or user have slow connection, when he press send, he have to wait until function finish his work.

I'm wondering, it is possible to create some kind of queque in Jquery.

For example i will get

function send_message()
{

//conditions
if, not empty, les then xx words, etc
//conditions

//if conditions ok
add_to_sedn_queque($somedata);
//if conditions ok

}

And that should be executed immediately.

*add_to_sedn_queque()* <- i just fake it, to show what i want to have.

So that function, should run my function with post sending. In order form oldest to newest requests.

I'm not asking You to write sending for me, i already have it working.

Just how create that queue, can be on simple example.

Regards

+1  A: 

I'd use .queue() :) Like this:

function add_to_send_queque(data) {
   $(document).queue('ajaxQueue', function(n) {
      $.ajax({
        url: "something",
        type: "POST",
        data: data,
        complete: n
   });
}

What this does is stick your calls on a custom queue up on document called ajaxQueue. As each request finishes (add a success method if needed, it won't affect this), it calls the next item in the queue, or leaves the queue open for the next one to be executed immediately (if the one that just finished was the last one queued).

Make sure to use complete here and not success for calling n, so it fires regardless of if there was an error...otherwise your queue will hang there waiting.

Or, if you need/want more options/control, there are plugins that do this and a bit more, the ajaxManager plugin is one of the top options here.

Nick Craver