按设定的时间间隔循环遍历 JSON feed 项目并替换上一个项目
我编写了一些 jQuery 来从 JSON 请求获取每个项目并插入到 HTML 表中,这只插入一次。我希望能够隐藏前一项,并在连续循环中一次插入 JSON 请求中的下一项,例如每 3 秒一次。我尝试过循环,但无法让它工作,所以只是寻求一些帮助!
到目前为止我所拥有的: http://jsfiddle.net/saffad/tZxKr/1/
JSON 请求如下所示:
{
"results": [
{
"member_photo": {
"thumb_link": "foo.jpeg"
},
"member_name": "Daniel",
"comment": "this is cool"
},
{
"member_photo": {
"thumb_link": "blah.jpeg"
},
"member_name": "John",
"comment": "hello everyone"
},
....
]
}
I've written some jQuery to get each item from JSON request and insert to an HTML table, this inserts it only once. I want to be able to hide the previous item and insert the next item from JSON request one at a time in a continuous loop, every 3 seconds for example. I've had a go at trying loops but I couldn't get it to work, so just asking for some help on this!
What I have so far: http://jsfiddle.net/saffad/tZxKr/1/
JSON request looks like this:
{
"results": [
{
"member_photo": {
"thumb_link": "foo.jpeg"
},
"member_name": "Daniel",
"comment": "this is cool"
},
{
"member_photo": {
"thumb_link": "blah.jpeg"
},
"member_name": "John",
"comment": "hello everyone"
},
....
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需执行以下操作:
编辑:现在它会连续循环。
我在你的 jsfiddle 中测试了它并且运行正常:)
希望这有帮助。干杯
Just do this:
EDIT: Now it loops continously.
I tested it in your jsfiddle and runs ok :)
Hope this helps. Cheers
如果你每 3 秒收到一条评论,我会尝试附加到一个 div 中。
评论div需要有一个区分id。该 id 可以是评论的 id。
您可能需要查找您处理的最后一条评论的 ID。
您可以存储在隐藏字段或某些全局变量中。
一旦您有新评论,您就可以追加新评论。
然后隐藏最后处理的评论。
这应该很简单。
If you are getting comments every 3 seconds, I would try to append to a div.
The comment div needs to have a distinguishing id. That id can be the id of the comment.
You might need to find the id of the last comment you processed.
Either you can store in a hidden field or some global var.
Once you have a new comment in, you append the new one.
You then hide the comment you processed last.
This should be simple.
该问题的接受答案并未满足用户实际要求。这是我的做法。更新的小提琴在这里
The accepted answer for the question doesn't do what the user actually asked for. Here is my go which does. There updated fiddle is here