看看我做的这个sn-p,我想它会给你带来一些想法:
http://jsfiddle.net/TgTtV/
lastDate = false;
setInterval(function() {
function compareDates (argument) {
return (new Date(argument.created_at) > lastDate);
}
jQuery.ajax({
url: 'http://search.twitter.com/search.json',
type: 'GET',
dataType: 'jsonp',
data: {q: encodeURIComponent('#test')},
success: function(data, textStatus, xhr) {
//If it's the first time running, prepend all tweets to the page
if(!lastDate) {
for(i in data.results) {
$('body').prepend('<p>'+data.results[i].from_user+': '+data.results[i].text+'</p>');
}
} else { //Only new tweets, filtered by date are added
var newMessages = data.results.filter(compareDates);
//Just for test
console.log(newMessages);
for(i in newMessages) {
$('body').prepend('<p>'+newMessages[i].from_user+': '+newMessages[i].text+'</p>');
}
}
lastDate = new Date(data.results[0].created_at);
}
});
}, 10000);
说明:
sn-p 首先从您的查询中加载一堆推文(在本例中为 #test 标签),然后将用户名和推文内容放在一个段落中。第一次加载后,它继续加载推文,但使用 compareDates 函数按日期过滤它们,并在页面中仅显示新推文。
为了更好地理解,我建议您在 Firebug 的控制台打开的情况下测试这个 sn-p。