Real-time ajax request

2

Well folks I have a script to do ajax request every 1 second to show me new content but now I am having a problem that it every 1 second updates me the whole content I wanted to only show up if there is a new one content in the database.

The script I'm using and this

<script>
//LISTA POSTS ESTABELECIMENTOS
    $(document).ready(function() {
    $.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
    setInterval(function() {
    $('#mostra_posts').load('ajax/mostra_posts.php?id_estabelecimento=<?php echo $row->id; ?>');
    }, 1000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
    });
    // ]]>
</script>

I would like to know how I can use this script.

    
asked by anonymous 21.02.2015 / 00:24

2 answers

1

The ideal would be to create another ajax service that notifies you if there is a change or not, if the change is detected only then make the request to update the new content.

An example would be:

var auto_atualiza = setInterval(function () {
  $.get('possui_alteracao.php', function(data) {
    if (data.possui) {
      $('#meudiv').load('listadados.php');
    }
  });
}, 30000);

If you do not want to do this approach another one would be to check if there is a change in the DOM, like this:

var cachedDOM = $('#meudiv').html();
var auto_atualiza = setInterval(function () {
  $.get('listadados.php', function(data) {
    if(data != cachedDOM) {
      $('#meudiv').html(data);
      cachedDOM = $('#meudiv').html();
    }
  });
}, 30000);

If you need more advanced DOM monitoring features follow a link

    
21.02.2015 / 01:34
1

You are using a technique called pooling that is asking the server from time to time if you have new content, this in itself is already heavy and if every time your request hits the server, the bank will become even heavier, this can leave your server somewhat overwhelmed. If so, consider using websockets. Here are two links that might be helpful in the socket client and socket server a>

    
21.02.2015 / 03:05