自动更新页面的代码大纲

发布于 2024-08-22 13:11:19 字数 300 浏览 8 评论 0原文

我想弄乱实时信息,并且有一个非常标准的功能我想复制:

当您在单个问题视图中输入答案时,它会出现在此处,并在顶部弹出一个警报,显示“有 3 条新闻答案,点击显示”

它也出现在 Twitter 上 “此搜索中有 5 条新推文:点击更新”

我非常精通服务器和客户端代码,我正在寻找的是此类事情如何发生的基本概要(甚至不是伪代码,而是简单的英语)。

服务器上是否有一个 CRON 作业每分钟运行一次,向页面上的长轮询 AJAX 位发送信号?

页面本身是否轮询服务器?

欢迎任何和所有解决方案。谢谢!

I want to mess around with realtime information, and there is a pretty standard functionality that I want to duplicate:

It occurs here on SO when you're on a single-question view, typing your answer, and an alert pops up top saying "there are 3 news answers, click to show"

It also occurs on Twitter "There are 5 new tweets in this search: click to update"

I'm pretty versed in server and client side code, and what I'm looking for is the basic outline (not even psuedo code, but perhaps plain english) of how something like this happens.

Is there a CRON job on the server running every minute, that shoots a signal to a long-polled AJAX bit on the page?

Does the page itself poll a server?

Any and all solutions are welcome. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懷念過去 2024-08-29 13:11:19

您可以通过使用 Javascript setTimeout 方法在客户端定期运行的 AJAX 调用来实现这一点。您将有一个 Javascript 函数,该函数调用服务器端方法来检查是否发生更新、显示任何更新,然后调用 setTimeout 来调用自身。

伪代码:

function updateCheck()
{
  //make ajax call
  //do something if any update has occurred
  setTimeout("updateCheck()", 10000); //second param is in milliseconds
}

You can implement that using an AJAX call that runs on the client side at a regular interval using the Javascript setTimeout method. You'll have a Javascript function that calls your server side method that checks if an update has occurred, displays any update, then calls setTimeout to call itself.

pseudo code:

function updateCheck()
{
  //make ajax call
  //do something if any update has occurred
  setTimeout("updateCheck()", 10000); //second param is in milliseconds
}
还不是爱你 2024-08-29 13:11:19

从我的头脑中,我会通过 javascript 来实现它 - 设置超时来询问服务器。但这只是一个有根据的猜测。

From the top of my head, I'd make it via javascript - setting timeouts to question the server. That's only an educated guess though.

简单爱 2024-08-29 13:11:19

看起来SO使用定期更新程序向如下网址发出ajax请求:

https://stackoverflow.com/posts /2307584/answer-activity-heartbeat

这将返回一个 JSON 结果:

{"Result":false,"Count":0}

以下是存在新答案时的结果示例:

{"Result":true,"Count":1}

Looks like SO uses a periodical updater to make an ajax request to a url like:

https://stackoverflow.com/posts/2307584/answer-activity-heartbeat

This returns a JSON result:

{"Result":false,"Count":0}

Heres an example of the result when a new answer exists:

{"Result":true,"Count":1}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文