简体   繁体   English

使用jQuery的网页上的实时数据

[英]Real-time data on webpage with jQuery

I would like a webpage that constantly updates a graph with new data as it arrives. 我想要一个不断更新新数据的图形的网页。 Regularly, all the data you have is passed to the page at the beginning of the request. 通常,您拥有的所有数据都会在请求开始时传递到页面。 However, I need the page to be able to update itself with fresh information every few seconds to redraw the graph. 但是,我需要页面能够每隔几秒钟更新一次新信息以重绘图形。

Background 背景

The webpage will be similar to this http://www.panic.com/blog/2010/03/the-panic-status-board/ . 该网页将类似于此http://www.panic.com/blog/2010/03/the-panic-status-board/ The data coming in will temperature values to be graphed measured by an Arduino and saved to the Django database (this part is already complete). 输入的数据将由Arduino测量的温度值绘制成图表,并保存到Django数据库中(这部分已经完成)。

Update 更新资料

It sounds as though the solution is to use the jQuery.ajax() function ( http://api.jquery.com/jQuery.ajax/ ) with a function as the .complete callback that will schedule another request several seconds later to a URL that will return the data in JSON format. 听起来解决方案似乎是使用jQuery.ajax()函数( http://api.jquery.com/jQuery.ajax/ )和.complete回调函数,该函数将在几秒钟后安排另一个请求到将以JSON格式返回数据的URL。

How can that method be scheduled? 该方法如何安排? With the .delay() function? 使用.delay()函数?

So the page must perform periodic jQuery.ajax calls with a url parameter set to a server's URL where the latest up-to-data information (possibly just as an incremental delta from the last instant for which the client has info -- the client can send that instant as a query parameter in the Ajax call) is served, ideally in JSON form. 因此,该页面必须执行url参数设置为服务器URL的定期jQuery.ajax调用,其中要获取最新的最新数据信息(可能只是从客户端获得信息的最后一刻起的增量增量,客户端可以发送该即时消息作为Ajax调用中的查询参数),最好以JSON形式提供。 The callback at the completion of the async request can schedule the next ajax calls for a few seconds in the future, and then repaint the graph. 异步请求完成时的回调可以将未来的ajax调用安排在未来几秒钟,然后重新绘制图形。

The fact that, server-side, you're using Django, doesn't seem all that crucial -- the server just needs to format a bunch of data as JSON and send it back, after all. 在服务器端,您正在使用Django,这一事实似乎并不那么重要-服务器毕竟只需要将一堆数据格式化为JSON并将其发送回去。

If the data is composed by on-the-fly graphics, you can just serve them (in gif, png or any other graphic format) through Django and reload each individually or even reload all the page at once. 如果数据是由即时图形组成的,则可以通过Django提供它们(以gif,png或任何其他图形格式),然后分别重新加载每个页面,甚至一次重新加载所有页面。

It depends on the load and performance requirements, but you can start simply reloading all the page and then, if necessary, use AJAX to reload just each specific part (it's very easy to achieve with jQuery or prototype using updates). 它取决于负载和性能要求,但是您可以开始简单地重新加载所有页面,然后在必要时使用AJAX仅重新加载每个特定部分(使用更新的jQueryprototype非常容易实现)。 If you expect a lot of data, then you should change to generate the graphic on the client, using just JSON incremental data. 如果期望大量数据,则应更改为仅使用JSON增量数据在客户端上生成图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM