Script creating multiple divs

0

I'm using javascript to update a div every 5 seconds, and every 5 seconds the div updates, but is creating several div equal.

I have the following code:

session_start();
$url ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$_SESSION['URL'] = $url;

echo("    <script>
$(document).ready(function(){
  $('#chatDiv').load('$url');

  var refreshId = setInterval(function(){
      $('#chatDiv').load('$url');}, 5000);
  $.ajaxSetup({ cache: false});
  });
</script>");
    
asked by anonymous 03.12.2017 / 16:21

1 answer

0

I suggest using $.get , and so you can filter what returns and get only content from div :

session_start();
$url ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$_SESSION['URL'] = $url;

echo("    <script>
$(document).ready(function(){
   $.get( '$url', function(data){
      $('#chatDiv').html( $(data).filter('#chatDiv').html() );
   });

  var refreshId = setInterval(function(){
      $.get( '$url', function(data){ $('#chatDiv').html( $(data).filter('#chatDiv').html() ); })}, 5000);
  $.ajaxSetup({ cache: false});
  });
</script>");
    
03.12.2017 / 18:47