What are the best practices for handling periodic updates from a PHP script using JavaScript in a web application?

Periodic updates from a PHP script in a web application can be handled using JavaScript by making AJAX calls at regular intervals to fetch updated data from the server. This can be achieved by using setInterval() function in JavaScript to trigger the AJAX call at specific time intervals. The fetched data can then be displayed on the webpage without the need for manual refresh.

<?php
// PHP script to fetch data and return JSON response
$data = // fetch data from database or API

header('Content-Type: application/json');
echo json_encode($data);
?>