Rob
Rob

Reputation: 69

ajax setInterval efficiency

I am using setInterval to run ajax in order to retrieve data from a database as close to real time as possible (every second). For now I have small amounts of data being pulled from the meeting_minutes_queries.php so there is no lag or glitches. I want to expand this code to multiple div containers on the same page creating multiple db connections. This seems very inefficient and possibly even dangerous. Is there a more efficient way to go about this process?

AJAX CODE:

setInterval(function(){
  function update_tb(){
    $.ajax({
      type: "POST",
      url: "meeting_minutes_queries.php",
      async: false,
      success: function(result){
        $(".slide_content1").fadeIn('slow').html(result);
      }
    }).responseText;
  }
  $(function(){
    update_tb();
  });
}, 1000);

Thanks for any help in advance.

Upvotes: 0

Views: 203

Answers (2)

Barlas Apaydin
Barlas Apaydin

Reputation: 7315

Try this:

$(function(){
  setInterval(function(){
    update_tb();
  }, 1000);

  function update_tb(){
    $.ajax({
      type: "POST",
      url: "meeting_minutes_queries.php",
      async: false,
      success: function(result){
        $(".slide_content1").fadeIn('slow').html(result);
      }
    }).responseText;
  }

});

Upvotes: 0

yankee
yankee

Reputation: 40810

Yes, there is.

You can use Long polling or WebSockets.

With slow poll you run a request and the server will keep the connection open indefinitely until it has something to say (e.g. in PHP sleep a lot and once you got a result, answer and be done).

With web sockets you have even more possibilities.

Upvotes: 2

Related Questions