Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 380
Clinton Jooooones Avatar asked Dec 13 '22 10:12

Clinton Jooooones


1 Answers

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);
like image 92
karim79 Avatar answered Dec 24 '22 08:12

karim79