Motive
Motive

Reputation: 3111

How to do live updates on page

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?

Upvotes: 0

Views: 3321

Answers (1)

Chris Clower
Chris Clower

Reputation: 5104

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

Upvotes: 1

Related Questions