输出记录使用Ajax实时计数

I'm working with ajax for the first time and I'm just experimenting with it for now. Eventually I want to trigger a notification on my website based on the number of records in a Mysqli table.

$result = $MySQLi_CON->query($sql); 
$sql = "SELECT * FROM posts";
<div id="div1">
    <?php 
    $row_cnt = mysqli_num_rows($result);
    printf("there are %d rows.
", $row_cnt);
    ?>
</div>

  <script type="text/javascript">
$(document).ready(function(){
  refreshTable();
});

function refreshTable(){
    $('#div1').load('backendform.php', function(){
       setTimeout(refreshTable, 5000);
    });
}
</script>

This outputs the number of records in the Posts table accurately after the page has reloaded. But I want to implement ajax so that it always checks what the number is, not just after the page has reloaded. So how to I output that number real-time using ajax?