views:

35

answers:

2

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.

A: 

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

John McCollum
+3  A: 

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.

Darin Dimitrov
+1 for great answer
Zafer
Nice, that would work, thank you.
Steven
i would also recommend using a good cacheing scheme on the server to minimize database calls also, thus making the server 'mass call' all the lighter
jim