jQuery(document).ready(function() { // don't wait 5 sec the first time just make the call do_request(); // periodically call the whoson service var periodic_request = setInterval (do_request, 5000 ); function do_request() { jQuery.ajax({ url : domain_root + '/simsite/php/whoson.php', type : 'get', dataType : 'json', success : function(json) { var whoson_container = jQuery('div#whoson'); whoson_container.replaceWith(json.content); if(json.status != 'timeout') { // if the whoson server didn't time out stop requesting periodical by clearing the interval clearInterval (periodic_request); } } }); } });