使用元刷新更新页面的替代方法
过去,当我报道事件时,我使用带有 5 分钟计时器的元刷新来刷新页面,以便人们获得最新更新。
意识到这可能不是完美的方法(在 IE 中并不总是有效,会中断一个人的流程,为使用屏幕阅读器的人重新启动事物等)我想知道是否还有其他方法可以处理这个问题情况。
如果服务器上的 html 文件较新,是否可以让像 ajax 这样的东西每隔几分钟检查一次,并让它打印一条消息说“更新信息可用,单击此处刷新”?
如果这太疯狂了,那么用 javascript 来从 5 分钟开始倒计时并建议刷新怎么样?
如果有人可以向我指出教程或代码片段,我将不胜感激。我只是在电视上扮演程序员。 :-)
In the past, when I've covered events, I've used a meta-refresh with a 5 minute timer to refresh the page so people have the latest updates.
Realizing that this may not be the perfect way to do it (doesn't always work in IE, interrupts a person's flow, restarts things for people with screen readers, etc.) I'm wondering if there's any other way to do handle this situation.
Is it possible to have something like ajax check every few minutes if the html file on the server is newer and have it print a message saying "Update info available, click here to refresh"?
If that's crazy, how about a javascript that just counts down from 5 minutes and just suggests a refresh.
If anyone could point me to tutorials or code snippets I'd appreciate. I just play a programmer on TV. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,您关于定时 Ajax 测试的想法是一个好主意。我不确定这正是 StackOverflow 使用的,但它会定期检查是否已发布其他答案,并每隔一段时间向用户显示是否有更新。
我认为这是理想的,原因如下:
我的看法是 - 让 ajax 脚本将最新的帖子 ID 发送到检查新脚本的脚本。该脚本可以查询您的数据库以查看是否有任何新帖子以及有多少。它可以返回这个数字。如果有新帖子,请显示一些(非模态)消息,包括更新数量,并让用户决定如何处理。
Actually, your thought on a timed Ajax test is an excellent idea. I'm not sure that is exactly what StackOverflow uses, but it checks periodically to see if other answers have been posted and shows the user, on an interval, if there are updates.
I think this is ideal for these reasons:
My take on how - have the ajax script send off the latest post id to a script that checks for new scripts. This script can query your database to see if there are any new posts, and how many there are. It can return this number. If there are new posts, show some (non modal) message including the number of updates, and let the user decide what to do about it.
您可以在 JavaScript 中使用 setInterval 函数。
这是一个示例,
您将使用它将名称传递给实际刷新第一个参数页面的函数以及第二个参数刷新之间的毫秒数(5 分钟为 300000)。第三个参数lang是可选的
You can use the setInterval function in javascript.
here's a sample
You will use it passing a name to a function that actually refresh the page for the first param and the number of milliseconds between refresh for the second param (300000 for 5 minutes). The third parameter lang is optional
如果用户要与乐谱进行交互并单击某些内容,那么刷新乐谱上的页面会有点粗鲁。我认为做一些诸如页面已更新通知之类的事情是理想的。
我将使用 jQuery 并对服务器上的文件执行 ajax 调用或返回更新后的数据。如果它比抛出 Growl 消息更新
Gritter - jQuery Growl System
Growl 使用 Gritter 的演示
Growl 消息可能会出现任何更改、新分数然后该消息中的一个选项用于刷新和查看新结果。
jQuery Ajax 信息
If the user would be interacting with the scores and clicking on things it would be a little rude to just refresh the page on them. I think doing something like a notification that the page has been updated would be ideal.
I would use jQuery and do an ajax call to the file on the server or something that will return the updated data. If it's newer than throw up a Growl message
Gritter - jQuery Growl System
Demo of what a Growl is using Gritter
A Growl message would come up possibly with whatever was changed, new scores and then an option within that message to refresh and view the new results.
jQuery Ajax information