ssbb
ssbb

Reputation: 1931

how to deal with slow Ajax request / people with slow connection

Here is a 'hypothetical' situation.

Let's say I have : a websocket who tell's me to send a ajax on a url like http://localhost/Whatever every 10 sec.

The ajax call on http://localhost/Whatever will take 45 seconde to reply (exagerate number for the situation).

I wonder how will the browser react? in my mind 3 case:

  1. (good one): Browser is really smart : He understand we ajax the same url so he won't stack ajax call until the current call finished.
  2. Browser understand we ajax the same url and make an abort() on the Ajax 1st call and start a new one (if the browser really does that, it would be rubbish in my mind).
  3. (worst case): Browser send a ajax on the url each time websocket ask him to and wait for the answer. Moreover, I suppose there will be a problem with limitation of parralel ajax request? (i wonder how the browser if this case happens ?)

So, Which case will happen and why ?

Upvotes: 0

Views: 1216

Answers (2)

bhspencer
bhspencer

Reputation: 13570

The answer is case 3.

The browser will send all requests in the order you make them. Generally speaking a computer will carry out your instructions in the order your issue them. If you want or need special behavior such as throttling the rate of the requests or not sending the subsequent requests until prior ones have finished you will need to implement that your self.

Upvotes: 2

Miraage
Miraage

Reputation: 3464

Imho, this pseudocode might help you.

var requestLock = false;

function pollFromSocket() {
  if (someCondition) {
    sendRequest();
  }
}

function sendRequest() {
  if (requestLock) {
    return;
  }

  requestLock = true;

  $.get('/whatever')
    .done(function(response) {
      // process response
    })
    .always(function() {
      requestLock = false;
    });
}

Upvotes: 1

Related Questions