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

时间:2022-09-09 12:37:27

Why is the data in the cycle is duplicated each time you update (after 5 seconds). displayed:

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

2016-09-09 09:12:18 WARN SZ SIU05 [main] Started, locked port 7075
2016-09-09 09:37:03 WARN SZ SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN SZ SIU05 [main] Started, locked port 7075
2016-09-09 09:37:03 WARN SZ SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 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 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]开始,锁定端口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 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口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 [主要]开始,l ocked port 7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口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

I need without doubles:

我需要没有双打:

2016-09-09 09:12:18 WARN SZ SIU05 [main] Started, locked port 7075
2016-09-09 09:37:03 WARN SZ SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 7075
2016-09-09 09:12:18 WARN MRP SIU05 [main] Started, locked port 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 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]开始,锁定端口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 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定端口7075 2016-09-09 09:12:18 WARN MRP SIU05 [main]启动,锁定港口7075

The HTML:

<ul></ul>

The script:

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

Output from api2.php:

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 个解决方案

#1


0  

Add $('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);
                });
                }

#1


0  

Add $('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);
                });
                }