几个用户每X分钟更新一次AJAX内容

时间:2022-09-24 01:08:05

So I have an area on my page that I would like to update every 10 minutes.

所以我的页面上有一个区域我想每10分钟更新一次。

Right now, I'm using this code:

现在,我正在使用这个代码:

var refresh = setInterval(refreshArea, 600000);

But this updates on the client side, so if 100 different users are looking at the page, the content will update at 100 different times.

但是这在客户端是更新的,所以如果有100个不同的用户在查看页面,那么内容将更新100次。

I'd like for the content to update every 10 minutes for all users, like at 3:00, 3:10, 3:20, etc., so that if a user comes to the page at 3:05, the content will update after 5 minutes, and then every 10 minutes after that.

我希望所有用户每10分钟更新一次内容,比如3点、3点10分、3点20分等等,这样,如果用户在3点05分到达页面,内容将在5分钟后更新,然后每10分钟更新一次。

I'm using ASP.NET MVC, so I'm sure there's some server code that I would need to do this, but I don't know how.

我用ASP。NET MVC,我确定有一些服务器代码我需要这样做,但是我不知道怎么做。

2 个解决方案

#1


3  

You could achieve this by combining the setTimeout and setInterval functions.

您可以通过组合setTimeout和setInterval函数来实现这一点。

var timeout = <%= (10 - DateTime.Now.Minute % 10) * 1000 %>;
setTimeout(function() {
    refreshArea();
    setInterval(refreshArea, 60 * 10 * 1000);
}, timeout);

Notice that the timeout value is calculated on the server.

注意,超时值是在服务器上计算的。

#2


0  

You could look at some sort of Comet solution for this problem; although I'm not too familiar with the Microsoft stack, Websync seems to do what you're looking for. (Although it's quite pricey.)

你可以看看这个问题的彗星解;虽然我不太熟悉微软栈,Websync似乎可以做你想做的事情。(虽然很昂贵。)

Alternatively, pokein offers a free alternative.

或者,pokein提供了一个免费的选择。

Disclaimer: I've never used either. :)

免责声明:我也没用过。:)

#1


3  

You could achieve this by combining the setTimeout and setInterval functions.

您可以通过组合setTimeout和setInterval函数来实现这一点。

var timeout = <%= (10 - DateTime.Now.Minute % 10) * 1000 %>;
setTimeout(function() {
    refreshArea();
    setInterval(refreshArea, 60 * 10 * 1000);
}, timeout);

Notice that the timeout value is calculated on the server.

注意,超时值是在服务器上计算的。

#2


0  

You could look at some sort of Comet solution for this problem; although I'm not too familiar with the Microsoft stack, Websync seems to do what you're looking for. (Although it's quite pricey.)

你可以看看这个问题的彗星解;虽然我不太熟悉微软栈,Websync似乎可以做你想做的事情。(虽然很昂贵。)

Alternatively, pokein offers a free alternative.

或者,pokein提供了一个免费的选择。

Disclaimer: I've never used either. :)

免责声明:我也没用过。:)