简体   繁体   English

如何使jQuery连续请求相同的HTTP GET请求

[英]How to make jQuery to continously request the same HTTP GET request

I am implementing a software that reads system status with jQuery by using $.getJSON . 我正在实现一个使用$ .getJSON读取jQuery系统状态的软件。

The idea is that I am displaying real-time data in browser and want to update the data as often as possible. 我的想法是我要在浏览器中显示实时数据,并希望尽可能频繁地更新数据。 The server is also running on localhost so theoretically I should be able to get very low latency (at least < 50 ms, even less than 20). 该服务器也在本地主机上运行,​​因此从理论上讲我应该能够获得非常低的延迟(至少<50 ms,甚至少于20 ms)。

But question is this: how can I implement getJSON with jQuery so that the HTTP GET request will be automatically resend straight after the earlier request was finished? 但是问题是这样的:我如何用jQuery实现getJSON,以便HTTP GET请求在较早的请求完成后立即自动重新发送?

In my current implementation I have defined an update function: 在当前的实现中,我定义了一个更新函数:

function updateSimulation() {       
   $.getJSON('/simulation/', function(data) {
      // ..parse data here
   });    
}

And added a callback once every 50 ms: 并每50毫秒添加一次回调:

document.simulationIntervalId = setInterval( "updateSimulation()", 50 );

The problem is that if a request takes longer than 50 ms, several requests will be waiting for a result asynchronously, meaning that if one request takes longer, older data maybe replaced with newer data, which is not desirable. 问题在于,如果一个请求花费的时间超过50毫秒,则多个请求将异步等待结果,这意味着如果一个请求花费的时间更长,则较旧的数据可能会被较新的数据替换,这是不希望的。

So basically it means one of the following two solutions could be consider: 1) either when a response arrives, all pending requests could be killed (not optimal), 2) a new request is only sent after the previous one has returned (perhaps optimal solution). 因此,基本上,这意味着可以考虑以下两种解决方案之一:1)当响应到达时,所有挂起的请求都可能被杀死(不是最佳的),2)仅在上一个请求返回后才发送新请求(也许是最佳的)解)。

But as always, all comments and answers are much appreciated! 但是与往常一样,所有评论和答案都非常感谢!

Why don't you call updateSimulation on the success function (the second parameter of $.getJSON) ? 为什么不对成功函数($ .getJSON的第二个参数)调用updateSimulation?

It will do the request after the previous one has finished. 上一个请求完成后,它将执行请求。

Firstly, when using setInterval() or setTimeout() , don't pass the method in as a string, as this calls the infamous eval() which is poor coding practice. 首先,在使用setInterval()setTimeout() ,请勿将方法作为字符串传递,因为这会调用臭名昭著的eval() ,这是不良的编码实践。 Rather pass the method like this: setInterval(updateSimulation, 50); 而是这样传递方法: setInterval(updateSimulation, 50);

To make sure the ajax request has completed before another one is sent off, use setTimeout() to only call the updateSimulation() method once, and then on the jquery's ajax success event, call updateSimulation() again. 为了确保ajax请求在发送另一个请求之前已经完成,请使用setTimeout()仅调用一次updateSimulation()方法,然后在jquery的ajax成功事件上再次调用updateSimulation()

Parse and use data first, then issue a deferred call 首先解析并使用数据,然后发出延迟的呼叫

Use window.setTimeout() instead and reissue another one after you've done processing the current one. 处理完当前window.setTimeout()后,请改用window.setTimeout()并重新发出另一个。 This way there will be no overlapping . 这样就不会有重叠

function updateSimulation() {
   $.getJSON('/simulation/', function(data) {
      // ..parse data here
      window.setTimeout(updateSimulation, 250);
   });
}

Adjust timing as you wish... 根据需要调整时间...

use setTimeout instead, and call it on return of precedent call. 请改用setTimeout,并在先例调用返回时调用它。 It will run 50 ms after return (btw, 50 ms is far too small for AJAX, think about 500 ms or 1000 ) 返回后将运行50毫秒(顺便说一句,50毫秒对于AJAX来说太小了,请考虑500毫秒或1000)

You can also add timestamp to sent data, and compare it against last known timeout 您还可以将时间戳添加到发送的数据,并将其与上一个已知的超时进行比较

function updateSimulation() {               
   $.getJSON('/simulation/', function(data) {
         window.setTimeout(updateSimulation, 500 );

         if (data.sent > last_data_sent){
           // use data
           last_data_sent = data.sent

         }

   });              
}

You also could use the setInterval with the period you want (50ms for example) and add an indicator to know if the previous request has finished. 您还可以将setInterval与所需的时间段(例如50ms)一起使用,并添加一个指示器以了解上一个请求是否已完成。 This indicator doesn't have to be in the gobal array thought. 该指标不一定必须包含在全局数组中。

function updateSimulation() {
   if (!requesting) {
      requesting = true;
      $.getJSON('/simulation/', function(data) {
         requesting = false;
      });
   }
}

var requesting = false;
setInterval ('updateSimulation', 50);

Where namespace is an object you use. 命名空间是您使用的对象。

2) sounds good, but 50ms is a little bit quick. 2)听起来不错,但50ms有点快。 I don't think that the Server will like it. 我不认为服务器会喜欢它。

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

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