Clinton Jooooones
Clinton Jooooones

Reputation: 1037

AJAX Polling - Check for new database entry

I'm polling my database every one second and I need it to do something only after a new entry has been submitted to the database; it can't just re-pull everything. Any help?

Upvotes: 4

Views: 3637

Answers (1)

karim79
karim79

Reputation: 342775

You can periodically check if the latest record ID matches the last ID pulled by your script, and if not, pull the new data. Example:

function updateView(id) {
    $.get("foo.php", { lastId: id }, function(response) {
        if(response != lastId) {

            // new entry in DB, do something special
            // and set lastId to the newly fetched ID
            lastId = id;
        }
    });
}

var i = setInterval(function() { updateView(id) }, 10000);

Upvotes: 13

Related Questions