If you want it to be updated at intervals, you can use setInterval
setInterval( refreshMessages, 1000 );
1000 - 1000 milliseconds, so for 1 second change this as you like.
So, every 1 second it calls the refreshMessages function:
function refreshMessages()
{
$.ajax({
url: 'messages.php',
type: 'GET',
dataType: 'html'
})
.done(function( data ) {
$('#msgs').html( data );
})
.fail(function() {
$('#msgs').prepend('Error retrieving new messages..');
});
}
source
share