Я не знаю, как ваш сервер обслуживает новые данные.
Учитывая статический текстовый файл с именем new_data.json
в том же каталоге, что и ваша страница, вы можете сделать следующий запрос ajax.
(Если вы обслуживаете страницу из файловой системы, некоторые браузеры могут доставить вам немного хлопот. Safari должен работать.)
Содержимое файла new_data.json
:
[ {"author":"@newAuthor","message":"newMessage","time":"newTime"},
{"author":"@anotherAuthor","message":"anotherMessage","time":"anotherTime"}
]
jQuery:
// Stores the latest request timestamp
var lastRequestTime = new Date();
// Make ajax request
$.ajax({
// URL of data, with the last time
url: 'new_data.json?time='+lastRequestTime,
dataType:'json',
success: function(data) {
// Update the lastRequestTime
lastRequestTime = new Date();
// Get the length of the array returned
var length = data.length;
// Walk backward through the array, adding each new item
// to the top of the container
while(length--) {
// Create new .container div
$('<div/>', {className:'container'})
// Append new divs to the $container with proper class and data.
// data[length][...] uses the current index stored in the length variable
.append( $('<div/>', {className:'author', text:data[length]['author']} ) )
.append( $('<div/>', {className:'message', text:data[length]['message']} ) )
.append( $('<div/>', {className:'time', text:data[length]['time']} ) )
// Prepend $container to the #content div
.prependTo( '#content' );
}
}
});