I have the following code that refreshes a PHP file every 9 seconds. This works ok but I would like to have a timeout in case a user leaves the page open. It should stop refreshing the call after 5 minutes otherwise it will keep loading my PHP file undefinitely wasting server resources. What is the most simple and efficient way to do this with jQuery?
Here is my code:
(function($)
{
$(document).ready(function()
{
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#content').show();
},
complete: function() {
$('#content').show();
},
success: function() {
$('#content').show();
}
});
const $container = $("#content");
$container.load("example.php");
const refreshId = setInterval(function () {
$container.load('example.php');
}, 9000);
});
})(jQuery);