What are the best practices for using PHP to refresh a page at regular intervals without impacting user experience?

To refresh a page at regular intervals without impacting user experience, it is best to use AJAX to asynchronously reload specific content on the page without reloading the entire page. This way, the user can continue interacting with the page without interruptions.

<script>
  setInterval(function(){
    $.ajax({
      url: 'refresh_content.php',
      success: function(data) {
        $('#content').html(data);
      }
    });
  }, 5000); // Refresh every 5 seconds
</script>