0

I have a page where users can signup to participate in a game match. A user just fills out a text field with a username and clicks a submit button.

Now, I can add their username to a table that shows all signed up users via ajax when they click submit.However, I'd like to have that table update without a page refresh every time a new user is added.

Is the only way to do this by setting some kind of interval that updates that table? It seems like it'd be making a lot of un-needed database calls. What's a good interval to use?

Should I store the table in a view and use .load to insert it into a div on the page every say, 10 seconds?

j08691
  • 204,283
  • 31
  • 260
  • 272
Motive
  • 3,071
  • 9
  • 40
  • 63
  • Try it, then see how it works. – SomeKittens Aug 04 '12 at 20:10
  • 2
    I think you want something like [long polling](http://stackoverflow.com/questions/333664/simple-long-polling-example-code), which more or less approximates push notifications in a browser. – Jared Farrish Aug 04 '12 at 20:13
  • Whats a good interval to use? That is up to you. How quickly do you need to find out about updates when someone signs up. – Josh Mein Aug 04 '12 at 20:13
  • Ah, long polling seems to be a good idea. Exactly why I asked this question instead of just doing the simple interval I had in mind. Pretty frustrating that I get downvoted by people like SomeKittens just because I'm seeking different solutions. – Motive Aug 04 '12 at 20:18
  • You have no idea who downvoted you. I didn't, @SomeKittens may or may not have, sometimes "free riders" see an opportunity. But don't be too confident in your blame. – Jared Farrish Aug 04 '12 at 20:20
  • Note as well that [nginx](http://nginx.org/) is more conducive to the kind of requests that this type of system will make (and the [slim framework](http://www.slimframework.com/)) than Apache, on scale. Literally, I think that's why those two were even developed. Buyer beware. – Jared Farrish Aug 04 '12 at 20:30

1 Answers1

1

You'll have to recheck for any changes on an interval:

setInterval(function() {
    $.ajax({
        url: "your/url",
        type: "GET",
        success: function(result) {
            // Update table
        }
    });
}, 10000); // Number of milliseconds to recheck
Chris Clower
  • 5,036
  • 2
  • 18
  • 29
  • 1
    Not [necessarily](http://stackoverflow.com/questions/333664/simple-long-polling-example-code), although that is an (*potentially* unattractive) option. I suggest `setTimeout()` plus long polling. – Jared Farrish Aug 04 '12 at 20:14