Steven
Steven

Reputation: 18859

Update content with AJAX every X minutes at same time for several users

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

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.

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.

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.

Upvotes: 1

Views: 873

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

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

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.

Upvotes: 3

John McCollum
John McCollum

Reputation: 5142

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.)

Alternatively, pokein offers a free alternative.

Disclaimer: I've never used either. :)

Upvotes: 0

Related Questions