0

我有一个 jQuery 脚本正在运行,它使用以下代码进行定期 AJAX 调用。

var a = moment();
var dayOfMonth = a.format("MMM Do");
var timeSubmitted = a.format("h:mm a");
var count_cases = -1;
var count_claimed = -1;

setInterval(function(){
    //check if new lead was added to the db
    $.ajax({
        type : "POST",
        url : "inc/new_lead_alerts_process.php",
        dataType: 'json',
        cache: false,
        success : function(response){

            $.getJSON("inc/new_lead_alerts_process.php", function(data) {

                if (count_cases != -1 && count_cases != data.count) {
                    window.location = "new_lead_alerts.php?id="+data.id;
                }
            count_cases = data.count;
            });
        }
    });

这是每次调用都会运行的 PHP:

$count = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM leads"));
$client_id = mysql_fetch_array(mysql_query("SELECT id, client_id FROM leads ORDER BY id DESC LIMIT 1"));

echo json_encode(array("count" => $count['count'], "id" => $client_id['id'], "client_id" => $client_id['client_id']));

我需要更改代码,以便仅在将新条目添加到数据库时触发警报,而不是在删除现有条目时触发。就目前而言,警报会针对这两个事件触发。

任何帮助是极大的赞赏。

4

2 回答 2

0

关于什么

var newest_id = 0;

window.setInterval(function() {
  $.ajax({
    ...
    success : function(data) {
      if (newest_id && data.id > newest_id)
         window.location = ...
      newest_id = data.id;
    }
  })
}, intervalTime);

你不必再打电话$.getJSON()了。它是$.ajax().

于 2012-10-25T19:44:21.973 回答
0

解决方案非常简单。我count_cases != data.count改为count_cases < data.count. 这就是它的全部。

于 2012-10-26T04:57:10.013 回答