Simple script sometimes not working

Simple script sometimes not working

Hello,
I have problem with very simple script. This code sometimes working, but sometimes I must refresh the page.

Script:
  1. <script>
  2.     $(document).ready(
  3.             function() {
  4. $("#loading").fadeOut('normal');
  5.                 setInterval(function() {
  6.                     $("#battlelist").load("rpsselect.php");
  7.                 }, 1000);
  8.             });
  9. </script>
PHP:
  1. $pdo = new PDO('there is host, password, user, and db name');
  2. $stmt = $pdo->prepare("SELECT id, defender, coins, defender_avatar FROM rpsbattles WHERE finished = 'NO' ORDER BY coins DESC");
  3. $stmt -> execute();
  4.         echo "<div id='battlelist'>";
  5. while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){
  6. $defender = $row['defender'];
  7. $coinsRPS = $row['coins'];
  8. $avatar = $row['defender_avatar'];
  9. $id = $row['id'];
  10. echo "<tr><th style='width: 340px;font-weight: normal !important;'><img src='".$avatar."' style='width:27px;margin-top: -3px;padding-right: 6px;'>".$defender."</th><th style='font-weight: normal !important;'>".$coinsRPS."</th><th><form method='POST'><input type='hidden' name='newID' id='newID' value='$id'><button type='button' name='graj' class='btn btn-primary btn-sm' data-toggle='modal' data-target='.rpsBattle' style='margin-top:-4px;margin-bottom:-3px;'><h5>GRAJ</h5></button></form></th>";
  11. }
  12.       echo "</div>";