Serializing POST jQuery / Javascript Requests

I am working on an application that sends an AJAX POST request (I'm currently using jQuery) every 1500 ms. I noticed that in most cases, these requests are executed within 350-450 ms and are sent to the server in the same order in which they are generated.

However, sometimes one or two requests take almost 3-4 seconds, and they are delivered later. In my application, I need to ensure that these requests are received by the server in the same order in which they were sent from the client. How should I do it? I am currently using 1500 ms setInterval to call a function (which generates data for publication) and which POST files use $ .ajax (). However, how do I serialize queries?

+3
source share
4 answers

I don’t know exactly how this will work in javascript, but you can create a list or an array of your request, first in the first list of types, so when you are ready, you click on your request or any values ​​necessary to create your request in an array, after the specified interval, which you take from the array and send the request. Basically you queue your request this way.

+2
source

One way: enable the timestamp sent using js, and on the receiving side, you can add each call to the queue , which is then executed after ordering it timestamp DESC.

, , , (, ?)

0

, 1500 , ...

$(function() {
  var recursive = arguments.callee;
  $.post("http://url", { /* data */ }, function() {
    setTimeout(recursive, 1500);
  });
});
0

.

  • ( ) .
  • setInterval, , .

. , - .

0

Source: https://habr.com/ru/post/1734600/


All Articles