Real-time request with ajax

Asked

Viewed 153 times

1

I need to update some information in real time, I did some research here on stackoverflow and found a very good post, but I could not make it work. In the post the following was explained:

Ajax that notifies if there is a change or not, if the change is detected only so make the request to update the new content:

1st Option:

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

2nd Option:

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

Doubts

Is the script correct? How does php code look for options 1 and 2?

Thanks.

  • What have you tested in PHP? There’s nothing else in the code you copied, if the reply script will accept.

  • The solution you are proposing may be efficient, but one tip is that instead of using setInterval is to use socket so that when there are changes on the server is already updated on the front. There are several discussions on this subject at Soen.

  • Perfect, I’ll switch to the socket. Another question is how the php code will look for it to work. Por exemplo: $pesquisa_sql_menu = "SELECT * FROM venda";
$select_menu = $pdo->prepare($pesquisa_sql_menu);
$executa_menu = $select_menu->execute();

while($reg_menu = $select_menu->fetch(PDO::FETCH_OBJ)){

echo $reg_menu->ap_qnt_bks;

}

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.