JQuery AJAX request is slow, although all previous requests are aborted

I have a search filter that runs an ajax request to update a result set when changing filters. It often happens that the user will change many filters at a time, selecting many AJAX requests.

So, I decided to abort previous requests if a new one was made (using the ajax.abort () function). Now ... Firebug shows all these requests as aborted, but the last request (the only one that is not interrupted) is more and more interrupted than before the requests were interrupted.

So, for example, if I change only one filter, it takes 1 second to load the request. But if I change 5 filters at a time, it will cancel 4 of them, leaving only the last request. However, this request ends in 5 seconds.

Does anyone know why this is? I assume that although jQuery interrupts the request, is my server still processing it? (I run local Rails Mongrel in development mode, fyi). This view strikes the whole reason why I interrupted requests ... What can I do to solve this problem?

Help is much appreciated!

+3
source share
1 answer

You might want to try the approach by delaying queries if user search parameters are entered quickly. For example, in one of my applications, I wait until the user stops for half a second.

  $("#user_login").keypress(function() {
    var login = this;
    if (this.value != this.lastValue) {
      $("#loading_small").addClass("loading-small");
      if (this.timer) { clearTimeout(this.timer); }
      this.timer = setTimeout(function() { 
        $.get("/users/login_validate", $(login).serialize(), function(data) {
          $("#loading_small").removeClass("loading-small");
          $("#login_valid").fadeIn().html(data);
        });
      }, 500);
    }
  });
+1
source

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


All Articles