Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page freezes for seconds when ajax post working

Tags:

jquery

ajax

When i use jquery's $.post ajax function, page freezes for 2-3 seconds and then data received. Freezing time can change depends on the data received.

How can i prevent this ?

EDIT:

COde i am using, it actually receives very large data

$.post("../ajax_updates.php", {  time: last_update }, function(data) { 
   if (data) {
   if (data != "") {
    $("#news_feed").prepend($(data).fadeIn('slow'));
    }
    }
    });
like image 466
Utku Dalmaz Avatar asked Dec 10 '22 01:12

Utku Dalmaz


1 Answers

If you load big amount of data through JavaScript this is normal, the problem is caused because your request is synchronous which will make your browser to wait this request to end before do anything else. You need to make your request asynchronous

P.S. Use $.get instead of $.post to get information from the server, in some cases - specially if you code work under Windows IIS you will get an error about that.

P.S-1. And it make sense $.get is for getting data from the server and $.post is for sending data.

Try this:

$.ajaxSetup({
    async: true
});

$.get("../ajax_updates.php", {  time: last_update }, function(data) { 
  if (data && data != "") {
    $("#news_feed").prepend($(data).fadeIn('slow'));
  }
});
like image 146
h4cky Avatar answered Dec 30 '22 14:12

h4cky