weixin_33688840 2015-06-26 17:43 采纳率: 0%
浏览 20

将JSON数据返回到表中

I have a script that returns data into a table, this works to a degree. The issue I am having is the script keep going round in some sort of loop and adding the same data to more rows in the table.

function refreshDataLiveEvents() {
    $.ajax({
        type: 'POST',
        url: 'check_current_events.php',
        data: $('#departcount').serialize(),
        dataType: "json", //to parse string into JSON object,
        success: function (data) {
            if (data) {
                var len = data.length;
                var txt = "";
                if (len > 0) {
                    for (var i = 0; i < len; i++) {

                        if (data[i].Department && data[i].Depart) {

                            txt += "<tr><td>" + data[i].Department + "</td><td>" + data[i].Depart + " </td></tr>";
                        }
                    }
                    if (txt != "") {
                        $("#table").append(txt);
                    }
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('error: ' + textStatus + ': ' + errorThrown);
        }
    });
    return false;

}
setInterval(refreshDataLiveEvents, 3000);

Can anyone see where or what I am doing wrong. Any help would be great. Many thanks in advance.

  • 写回答

2条回答 默认 最新

  • weixin_33709364 2015-06-26 17:48
    关注

    The loop is because you use setInterval that executes refreshDataLiveEvents every 3 seconds. If you need to execute it once after 3 seconds, use setTimeout instead.

    So just change this:

    setInterval(refreshDataLiveEvents, 3000);
    

    to this:

    setTimeout(refreshDataLiveEvents, 3000);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题