Javascript jquerylivefeed只显示10项
我最近发现 HTMLJavascript jquerylivefeed只显示10项,javascript,jquery,html,mysql,Javascript,Jquery,Html,Mysql,我最近发现 HTML <html> <head><title>Tweets</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <style> #tweets { width: 500px; font-fami
<html>
<head><title>Tweets</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
#tweets {
width: 500px;
font-family: Helvetica, Arial, sans-serif;
}
#tweets li {
background-color: #E5EECC;
margin: 2px;
list-style-type: none;
}
.author {
font-weight: bold
}
.date {
font-size: 10px;
}
</style>
<script>
jQuery(document).ready(function() {
setInterval("showNewTweets()", 1000);
});
function showNewTweets() {
$.getJSON("feed.php", null, function(data) {
if (data != null) {
$("#tweets").prepend($("<li><span class=\"author\">" + data.author + "</span> " + data.tweet + "<br /><span class=\"date\">" + data.date + "</span></li>").fadeIn("slow"));
}
});
}
</script>
推特
#推特{
宽度:500px;
字体系列:Helvetica、Arial、无衬线字体;
}
#李彦宏在推特上写道{
背景色:#E5EECC;
保证金:2倍;
列表样式类型:无;
}
.作者{
字体大小:粗体
}
.日期{
字体大小:10px;
}
jQuery(文档).ready(函数(){
setInterval(“showNewTweets()”,1000);
});
函数showNewWeets(){
$.getJSON(“feed.php”),null,函数(数据){
如果(数据!=null){
$(“#tweets”).prepend($(“”+data.author+“+data.tweet+”
“+data.date+” ”).fadeIn(“慢”);
}
});
}
PHP
<?php
echo json_encode(array( "author" => "someone",
"tweet" => "The time is: " . time(),
"date" => date('l jS \of F Y h:i:s A')));
?>
它对我来说非常有用,但我希望它经常(每3秒钟)查看是否有新的tweet可用,如果有,它应该将tweet添加到列表的顶部。列表最多只能有10个
项。如果有超过1条新tweet,它应该只添加1条新tweet,再过3秒,它应该加载另一条新tweet(如果可用)
如何添加此限制
谢谢
jQuery(文档).ready(函数(){
setInterval(“showNewTweets()”,1000);
});
函数showNewWeets(){
var tweets=0;
$(“#tweets”).html(“”);
$.getJSON(“feed.php”),null,函数(数据){
if(data!=null | |推特“This”确实是这样吗?@nicael在我的问题中写道,答案中的脚本被标记为解决方案。如果你真的提供了你感兴趣的代码片段,那会很有帮助。另外,介意向我们展示你的任何尝试吗?@CristianD我认为从另一个stackoverflow页面复制并粘贴脚本是愚蠢的,但如果你想让我添加它,我不会这么做o我的问题。我考虑过,但问题是整个列表都会消失并重新加载新列表(例如,如果在重新加载时间内有超过10个新项目),这导致了问题,即它看起来不好,一些推文正在丢失。显示最后10条推文的唯一方法是删除以前的结果,并在拨打电话时重新插入最后10条推文。我考虑显示10条推文,并不断(例如每3秒)在顶部添加一条新推文(如果有新的),总共应该有10条以上的推文显示。
<script>
jQuery(document).ready(function() {
setInterval("showNewTweets()", 1000);
});
function showNewTweets() {
var tweets = 0;
$("#tweets").html('');
$.getJSON("feed.php", null, function(data) {
if (data != null || tweets <= 10) {
$("#tweets").prepend($("<li><span class=\"author\">" + data.author + "</span> " + data.tweet + "<br /><span class=\"date\">" + data.date + "</span></li>").fadeIn("slow"));
tweets++;
}
});
}
</script>