数据输出在javascript中重复。怎么修?

时间:2016-09-10 04:17:21

标签: javascript php jquery ajax

为什么每次更新(5秒后)周期中的数据都会重复。 显示:

  

2016-09-09 09:12:18 WARN SZ SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:37:03 WARN SZ SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN SZ SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:37:03 WARN SZ SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075

我需要没有双打:

  

2016-09-09 09:12:18 WARN SZ SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:37:03 WARN SZ SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075
  2016-09-09 09:12:18 WARN MRP SIU05 [主要]启动,锁定端口7075

HTML:

<ul></ul>

剧本:

<script type='text/javascript'>
$(document).ready(function(){
    /* call the php that has the php array which is json_encoded */
    $.getJSON('api2.php', function(data) {
        /* data will hold the php array as a javascript object */
        $.each(data, function(key, val) {
            $('ul').append('<li id="' + key + '">' + val.first_name + ' ' + val.last_name + ' ' + val.email + ' ' + val.age + '</li>');
        });
    });
});
</script>

api2.php的输出:

[{"date":"2016-09-09 09:12:18","event":"WARN ","region":"SZ","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:37:03","event":"WARN ","region":"SZ","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"},{"date":"2016-09-09 09:12:18","event":"WARN ","region":"MRP","host":"SIU05","type":"[main]","info":"Started, locked port 7075"}]

1 个答案:

答案 0 :(得分:0)

添加$('ul').empty();

   function getData(){
                    $.getJSON('api.php', function(data) {
                            $('ul').empty();
                            /* data will hold the php array as a javascript object */
                            $.each(data, function(key, val) {
                                    $('ul').append('<li id="' + key + '">' + val.date + ' ' + val.event + ' ' + val.region + ' ' + val.host + ' '+ val.type + ' ' + val.info + '</li>');
                });
                setTimeout(getData, 5000);
                });
                }