T
T
ThreegunD2018-02-19 12:26:30
JavaScript
ThreegunD, 2018-02-19 12:26:30

Correct ajax request to get and concatenate data?

The bottom line is this:
By pressing a button, we make a request to the server, get the data and check if the data length is 500, then send it again (specify the indent of 500), etc. until the data is less than 500 (which means no more).
As a result, we get an array of data, for example, 4500
long. in asynchronous-data comes before all joins.
I would like to know how to do it correctly so as not to "freeze" the page for a few seconds (as with synchronous requests).
An example of how I do it now.

function getMails () {
  var mails = [];
  var page = 1;
  function request () {
        var result = false;
        $.ajax(
          url: 'какой-то урл/' + page,
          async: false,
          type: 'GET',
          success: function (data) {
            result = data.items;
          }
        });
        return result;
      }
      function gluing () {
        var part = request();
          mails = mails.concat(part);
        if (part.length >= 500) {
          page++;
          gluing();
        }
      }
gluing();
return mails;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question