添加数据库条目时触发警报,而不是在删除它们时触发警报

时间:2012-10-25 15:51:06

标签: php javascript jquery

我运行了一个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']));

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分: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()的快捷方式。

答案 1 :(得分:0)

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