I'm trying to use AJAX to change the content of a div of the page every 5 seconds. I've realised that it creates a loop that sends more requests every time. It changes the page content but spams requests.
I don't know how to fix it without breaking it even more.
$(document).ready(function() {
sendRequest();
function sendRequest() {
$.ajax({
url: "chat.txt",
success: function(data) {
$('.chat').html(data);
},
complete: function() {
setInterval(sendRequest, 5000);
}
});
};
});