I have a code that fetches an array of objects from the server and displays them in a list. Everything works fine here except I feel like the code is sloppy and not efficient. Could I implement setInterval somewhere in my AJAX call so I don't call setInterval at the end? Also if I call setInterval in my code how can I get the AJAX call to refresh but also display the strings I'm fetching from the server as soon as the page is opened? I am very new to AJAX and jQuery (first day trying AJAX) so as always any syntax/indentation editing is much appreciated!
function displayMessages(messages){
$('.messages').append('<li>' + messages + '</li>');
}
function fetchMessages(){
$.ajax({type: "GET",
url: "https://api.parse.com/1/classes/chats",
success: function(data) {
for(var i = 0 ; i < 10; i++){
displayMessages(data.results[i].text);
if($('.messages li').length > 10){
$('.messages li').first().remove();
}
}
}
});
}
fetchMessages();
setInterval(fetchMessages,3000);