Page freezes for a few seconds when ajax post works

When I use jquery's $ .post ajax function, the page freezes for 2-3 seconds and then receives data. Freezing time may vary depending on the received data.

How can I prevent this?

EDIT:

COde I am using it actually gets very big data

$.post("../ajax_updates.php", {  time: last_update }, function(data) { 
   if (data) {
   if (data != "") {
    $("#news_feed").prepend($(data).fadeIn('slow'));
    }
    }
    });

      

+3


source to share


2 answers


If you are loading a large amount of data via JavaScript, this is normal, the problem is due to your request synchronous

, which will make your browser wait for that request to complete before doing anything. You must make your requestasynchronous

PS Use $ .get instead of $ .post to get information from the server, in some cases - especially if you are working with the code in Windows IIS, you will get an error message.

PS-1. And it makes sense $ .get to get data from the server and $ .post to send data.



Try the following:

$.ajaxSetup({
    async: true
});

$.get("../ajax_updates.php", {  time: last_update }, function(data) { 
  if (data && data != "") {
    $("#news_feed").prepend($(data).fadeIn('slow'));
  }
});

      

+10


source


Make sure async is set to true when submitting ajax request. If it is set to false, the browser will freeze until it receives a response.



http://api.jquery.com/jQuery.ajax/

+2


source







All Articles