设置间隔替代方案
在我的应用程序中,我每秒轮询网络服务器以获取消息并将其显示在前端。 我使用 setInterval 来实现这一点。然而,只要用户停留在该页面上,即使没有数据,客户端也会不断向服务器发送请求。当不再通过设置变量生成消息时,服务器确实会给出指示。 我想过使用这个变量来清除间隔并停止计时器,但这不起作用。在这种情况下我还能使用什么? 我正在使用 jquery 和 django。这是我的代码:
jquery: var refresh = setInterval( function () { var toLoad = '/myMonitor'+' #content'; $('#content').load(toLoad).show(); }, 1000); // refresh every 1000 milliseconds }); html: div id=content is here
我可以在每次刷新时访问 django 变量以在 html 中完成。如果有的话我该如何设置clearInterval?
注意:堆栈溢出不允许我输入 is > < 所以 html 不完整
谢谢
更新于 03/16/2010 我一定是做错了什么。但无法弄清楚。这是我的带有clearTimer 的脚本,但它不起作用。
var timer = null; $(function(){ if ("{{status}}" == "False") { clearInterval(timer); } else { timer = setInterval( function(){ var toLoad = '/myMonitor'+' #content'; $('#content').load(toLoad).show();} ,1000); // refresh every 1000 milliseconds } });
status 是“views.py”(Django)中设置的布尔值。 非常感谢。
In my app I am polling the webserver for messages every second and displaying them in the frontend.
I use setInterval to achieve this. However as long as the user stays on that page the client keeps polling the server with requests even if there is no data. The server does give an indication when no more messages are being generated by setting a variable.
I thought of using this variable to clearInterval and stop the timer but that didn't work. What else can I use in this situation?
I am using jquery and django. Here is my code:
jquery: var refresh = setInterval( function () { var toLoad = '/myMonitor'+' #content'; $('#content').load(toLoad).show(); }, 1000); // refresh every 1000 milliseconds }); html: div id=content is here
I can access the django variable for completion in html with each refresh. How can I set clearInterval if at all ?
Note: stack overflow does not let me put is > < so html is incomplete
Thanks
Updated 03/16/2010
I must be doing something wrong. But cannot figure it out. Here is my script with clearTimer and it does not work.
var timer = null; $(function(){ if ("{{status}}" == "False") { clearInterval(timer); } else { timer = setInterval( function(){ var toLoad = '/myMonitor'+' #content'; $('#content').load(toLoad).show();} ,1000); // refresh every 1000 milliseconds } });
status is a boolean set in "views.py" (Django).
Thanks a bunch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几个人已经回答了您的问题的具体资源,所以我想我应该提供一些背景知识。
简而言之,您希望服务器将数据推送到浏览器以避免大量的客户端轮询。没有一个好的跨浏览器方法来支持服务器推送,因此需要更少轮询的常见解决方案是使用 Comet(另一种清洁产品,如 AJAX)长轮询技术。
使用 Comet,浏览器发出请求,服务器保持连接打开而不响应,直到有新数据可用。当服务器有新数据时,它会通过打开的连接发送该数据,浏览器会立即接收该数据。如果连接超时,浏览器将打开一个新连接。这使得服务器可以在数据可用时立即将数据发送到客户端。正如其他人所指出的,这种方法需要对您的网络服务器进行特殊配置。您需要在服务器上有一个脚本来定期检查数据并响应客户端(如果存在)。
使用这种方法需要记住的是,大多数 Web 服务器都是为了获取客户端的请求并尽快响应而构建的;它们不适合长期存活。使用 Comet,您将拥有比平常更多的开放连接,可能消耗比您预期更多的资源。
A couple people have already answered with specific resources to your problem, so I thought I would provide a bit of background.
In short, you want the server to push data to the browser to avoid extensive client-side polling. There isn't a good cross-browser way to support server push, so a common solution that requires much less polling is to use the Comet (another cleaning product, like AJAX) long-poll technique.
With Comet, the browser makes a request, and the server keeps the connection open without responding until new data is available. When the server does has new data, it sends it over the open connection and the browser receives it right away. If the connection times out, the browser opens a new one. This lets the server send data to the client as soon as it becomes available. As others have indicated, this approach requires special configuration of your web server. You need a script on the server that checks for data at an interval and responds to the client if it exists.
Something to keep in mind with this approach is that most web servers are built to get a request from a client and respond as quickly as possible; they're not intended to be kept alive for a long period of time. With Comet you'll have far more open connections than normal, probably consuming more resources than you expect.
您的clearInterval 检查仅在触发文档就绪事件时进行检查。
如果您提供的代码与浏览器中的代码完全相同,则您将字符串“{{status}}”与字符串“False”进行比较。我宁愿看着油漆变干,也不愿等待它被评估为真实。
如果您的请求完成时间超过 1 秒怎么办? :您的服务器将充满请求。
比您所在的位置更近,但您仍然需要弄清楚如何决定何时停止投票。
Your clearInterval check is only checking when the document ready event is fired.
If the code you gave is exactly what's in the browser, then you're comparing the string "{{status}}" to the string "False". I'd rather watch paint dry than wait for that to evaluate as true.
What if your requests taking longer than 1 second to complete? : You'll flood your server with requests.
Is closer than where you were, but you still need to work out how you're going to decide when you want to stop polling.