I'm on a project that has a need for a scrolling list of users online in a table. The idea is that the server is pinged for the entire list, it populates the first 5 and then removes the top and adds the next on a 2.5 second interval. After the entire list is gone through I need to hit up the server for the latest listing and repeat, forever.
The issue I'm having is the "forever" aspect. While the first load is fine and the loops works great, the second one has issues. If the //get_users_online_today(); is uncommented it appears as if the call takes places after the first setTimeout() trigger finishes.
I can't seem to figure out where to place the function inside the function to call it repeatedly.
$(function(){
function get_users_online_today(){
var user = 0;
$.ajax({
url: 'ajax.php',
data: {action: 'Users Online Today'},
dataType: 'json'
}).done(function(users){
$.each(users, function(index, details){
if($('#users_online_today tbody tr').length <= 5){
$('#users_online_today tbody').append('<tr><td>' + details.name + '</td></tr>');
}else{
setTimeout(function(){
$('#users_online_today tbody tr:first').remove();
$('#users_online_today tbody').append($('<tr><td>' + details.name + '</td></tr>').fadeIn(250));
}, (user++) * 2500);
}
});
//get_users_online_today();
});
}
get_users_online_today();
});