简体   繁体   English

如何制作一个脚本,用POST或GET每秒轮询一次而没有滞后?

[英]How to make a script that polls every second with POST or GET without lag?

I want to make a notification center for my control manager system. 我想为我的控制管理中心系统创建一个通知中心。 If I use something like this: 如果我使用这样的东西:

$(function(){
    setInterval(function(){
        $.post("url", { data: 'value' }, function(result){
            // check for updates
        });
    }, 1000);
});

the page is lagging all the time. 该页面一直处于滞后状态。 Isn't it possible to do something like this 'on the background'? 不可能在“后台”做类似的事情吗? Without loading or lagging issues? 没有加载或滞后问题? If that is possible, that would be great! 如果可能的话,那就太好了! I tried several things but didn't have much effect. 我尝试了几件事,但效果不大。

you should increase your interval time to something more reasonable than 10 times/second. 您应该将间隔时间增加到比10次/秒更合理的时间。 maybe something like once per second with a value of 1000 大概每秒一次,值1000

What you are referring to is called long-polling. 您所指的是长轮询。 Without knowing what your code is doing to the page, it is hard to tell what it causing lag on the page. 不知道您的代码对页面做了什么,很难说出它导致页面滞后的原因。 Even so, polling every second can put a big load on your server as you scale. 即便如此,每秒轮询仍会在您扩展服务器时给服务器带来很大的负担。

You will want to increase your time interval. 您将需要增加时间间隔。 Something like once every 3-10 seconds won't adversely affect the user experience. 每3-10秒一次的操作不会对用户体验产生不利影响。 People will not notice unless you are making a shared document editing or drawing application. 除非您要进行共享文档编辑或绘图应用程序,否则人们不会注意到。 It sounds like you are simply pushing notifications. 听起来您只是在推送通知。

If you want web "real time" notifications, use WebSockets. 如果您需要Web“实时”通知,请使用WebSockets。 You will still need to implement long-polling for browsers that do not support WebSockets: details here under 'Browser compatibilities' . 您仍然需要为不支持WebSockets的浏览器实施长轮询: “浏览器兼容性”下的详细信息

HTML5 Rocks has a good intro to WebSockets . HTML5 Rocks很好地介绍了WebSockets

If you want a guaranteed interval between your calls, instead of setInterval try using a setTimeout and call the same function that has your post/get code. 如果您希望在两次调用之间有一个确定的间隔,请尝试使用setTimeout而不是setInterval,并调用具有邮编/获取代码的相同函数。 Something like 就像是

function myTimeout() {
  //your code for post/get
  setTimeout(myTimeout, 1000);
}

What this does is guarantee the "Previous Interval" is complete before the next call to "myTimeout" which in turn has the post. 这是为了确保在下一次调用“ myTimeout”之前,“ Previous Interval”已完成,而后者又发出了帖子。 This I believe, would not be guaranteed using setInterval, which in your case calls a function making the post every 1000 milli sec no matter what. 我相信,使用setInterval不能保证,在您的情况下,setInterval会调用一个函数,使函数每1000毫秒发送一次,无论如何。 With delays, the calls may not be spaced out by the desired interval(1000 milli sec). 在延迟情况下,呼叫可能无法以所需的间隔(1000毫秒)间隔开。 This still does not guarantee a fixed interval between calls though(if the call/response takes longer time than the specified interval). 但是,这仍然不能保证两次呼叫之间的固定间隔(如果呼叫/响应花费的时间比指定间隔更长)。 Read more at https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval under the heading "Dangerous usage" 请在https://developer.mozilla.org/zh-CN/docs/Web/API/window.setInterval标题为“危险使用”下阅读更多信息。

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

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