如何使Ajax请求每次10秒(除长轮询)? [英] How to make Ajax requests each 10 seconds (besides long-polling)?

查看:252
本文介绍了如何使Ajax请求每次10秒(除长轮询)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让从服务器请求一个JSON对象使用这个每次10秒时:

I'm trying to get request a json object from the server each 10 seconds using this:

setInterval(function(){
  $.ajax({
    url: '/',
    success: function(data){
      //do stuff with data
    }
  });
}, 10000);

但是,这不是很有效。我知道长轮询,但我不认为它会带来很大的区别。我的知道的我将接收新的数据每10秒钟,所以不会使长轮询的完全相同的setInterval在效率方面?

But this isn't very efficient. I know about long-polling, but I don't think it'll make a big difference. I know I will receive new data each 10 seconds, so doesn't that make long-polling the exactly same as setInterval in terms of efficiency?

时的浏览器端的缓存对于这个问题一个很好的解决方案?

Is browser-side caching a good solution for this problem?

JSON对象我会得到这个样子的:

The JSON object I'll be getting looks like this:

var data =  {1: {'user': 'John', 'age': '25'}, {2: {'user': 'Doe', 'age': '30'}}

使用这一点,我想显示数据[0]的.user几秒钟,之后顺利它通过使用'淡出'和'淡入'等,直到它耗尽改变成数据[1]的.user用户。 我基本上要创建用户的用户名的幻灯片。

With this, I want to display data[0].user for a few seconds and after that smoothly change it into data[1].user by using 'fadeOut' and 'fadeIn' and so on until it runs out of users. I basically want to create a slideshow of the user's usernames.

缓存会是一个很好的解决方案,或者我应该坚持让AJAX调用每10秒?如果是,那么我将如何实现这一点,如果没有,我应该用什么方法?

Would caching be a good solution or should I stick with making ajax calls every 10 seconds? If so, then how would I implement this and if not, what method should I use?

我不知道如果我解释它不够好,所以告诉我,如果事情还不清楚。

I'm not sure if i explained it good enough, so tell me if something is still unclear.

推荐答案

我肯定会想到缓存。特别是如果你设置了更多的用户,使一个AJAX请求每10秒可以很容易超载的服务器。但是,如果你想保持简单,做一个请求,每隔几分钟,而不是来更新。高速缓存中的用户,让他们产生进入的JavaScript code,说用户=新的Array(用户1,用户2,...)。你不会真的有不断更新的网页,如果它不是那么重要,因为大多数用户将导航离开一两分钟内的反正。如果你有一个长长的名单,改变每隔几秒钟,这给了你足够的时间不曾经有使用AJAX来更新,而仅仅依靠用户生成的服务器列表中。

I would definitely think about caching. Especially if you get sets with a lot more users, making an AJAX request every 10 seconds could easily overload your server. However, if you want to keep it simple, make a request every few minutes instead, to update. Cache the users, have them generated into the javascript code, say users = new Array(user1, user2, ...). You don't really have to keep updating the page if it's not that important, as most users would navigate away within a minute or two anyways. If you have a long list that changes every few seconds, that gives you enough time to not ever have to update using AJAX, and just rely on the server generated list of users.

如果没有,你保存的更新列表中的变量最后一次,并发送时间,当你通过AJAX要更新的参数到您的服务器,然后让服务器快速检查中添加哪些新的用户,发送只是那些。于是,刚刚合并的新服务器与旧阵列的新阵列。我强烈建议不要拨打电话每10秒为一个新的名字,但。你不仅会在服务器上运行更多的带宽,你会增加CPU使用率,当它已经在列表中找到适合你的下一个用户,然后给你一个。对于好的做法,始终让客户做很多的工作,尽可能,无需滞后。只有一个服务器,但是有更多的客户端。每个操作你转移到客户端将保存操作的服务器数百,甚至上千。

If not, store the last time you updated the list in a variable, and send the time as an argument to your server when you're updating via AJAX, and then have the server quickly check what new users were added, and send just those. Then, just merge the new array of new servers with the old array. I highly suggest not making a call every 10 seconds for a new name though. Not only will you run up more bandwidth on your server, you will increase the CPU usage when it has to find the next user in the list for you, and then send you that one. For good practice, always let the client do as much of the work as possible, without having lag. There is only one server, but many more clients. Each operation you shift to the clients will save your server hundreds, if not thousands, of operations.

对于长轮询VS setInterval的,我想在这种情况下推荐的setInterval。至少可以发送具有时间参数的请求,指定最后的更新时间,并因此只需要发送,而不是整个数据阵列小部分,

As for long polling vs setInterval, I would recommend setInterval in that case. You can at least send a request with a time argument, specifying the last update time, and thus only needing to send that small portion, instead of the entire data array.

var storage = new Array(user1, user2, ...); //set all your data here, generate it from your server
var lastUpdate = //set the last time you updated it, just create a date variable

function rotateUsers()
{
    //do your fade in and fade out here
}

function update()
{
    //create a new HttpRequest, and then set the url as "yoursite.com/update?lastUpdateTime="+lastUpdate;
    //Take the response data, and merge the new users list with the old one
}
setInterval('rotateUsers()',10000);
setInterval('update()',60000); //update once a minute

这篇关于如何使Ajax请求每次10秒(除长轮询)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆