无法查看从套接字到浏览器的事件

时间:2019-06-28 12:21:51

标签: javascript html google-chrome innerhtml

我有一个html客户端,那就是侦听来自JS服务器的事件。我可以确认在浏览器的客户端上我收到了事件,但是,事件没有显示在UI中。我想念什么?谢谢!

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>&#128293; Test Events</title>

    <!-- Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>

  <body>
    <div class="container">
      <div class="row">
        <div class="col-sm-7">
          <div id='timeline'></div>
        </div>
      </div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <script src="/socket.io/socket.io.js"></script>

    <script type="text/javascript">
      var events = []
      const socket = io()
      socket.on('test', function(data) {
        console.log(data)
        events.push(data)
        trimmed = events.slice(Math.max(events.length - 30, 1))
        html = trimmed.reverse().map(function(data){
          return `<div class="media">
            <div class="media-left">
              <h3 class="media-heading">Test</h3>
            </div>
            <div class="media-body">
              <h4 class="media-heading">${data}</h4>
            </div>
          </div>`
        }).join('')
        $("#timeline")[0].innerHTML  = html
      }, false)
    </script>

0 个答案:

没有答案