AJAX轮询 - 检查新数据库条目

时间:2022-09-26 00:08:59

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?

我每隔一秒轮询一次我的数据库,只有在新的条目提交到数据库之后我才需要它做一些事情;它不能只是重新拉动一切。有帮助吗?

1 个解决方案

#1


13  

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:

您可以定期检查最新记录ID是否与脚本提取的最后一个ID匹配,如果不匹配,请提取新数据。例:

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);

#1


13  

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:

您可以定期检查最新记录ID是否与脚本提取的最后一个ID匹配,如果不匹配,请提取新数据。例:

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);