使用套接字io将数据传递到html表

时间:2019-11-24 14:10:21

标签: javascript html websocket socket.io

我已经使用了API中的数据,并且该数据将使用socketio实时显示在表中,在那里它以对象的形式返回数据,例如示例:

"data": [
    {
      "name": "e7876319e4",
      "status_waiter": "Ready"
    },
    {
      "name": "e787631334",
      "status_waiter": "Ready"
    }
]

所以我想在HTML表中显示该数据,如果数据发生变化,该表也必须更改,我在其中进行了间隔设置,那我该怎么做?

我尝试使用$('#status').append(data)成功,但是即使没有任何更改,它仍然可以添加数据。

1 个答案:

答案 0 :(得分:1)

尝试:

$('#status').empty(); //removes existing
$('#status').append(data); //add new

AS:

function updateTable(){
    let data = []; // set data here
    $('#status').empty(); //removes existing
    $('#status').append(data); //add new

    setTimeout(()=>{updateTable();}, 1000); //call this after 1 second
}

// call this when ever you want to start the updatefor the first time, after that...
updateTable();