Javascript socket display new pushed data every k seconds

Viewed 19

I am new to JavaScript and I am trying to display list of messages pushed by websocket every K seconds. I am able to received data from the websocket and can view it using console.log

<html lang="EN">
<head>

    <script type="text/javascript">
        function myFunction() {

            if ("WebSocket" in window) {
                const ws = new WebSocket("ws://localhost:9000/test");
                ws.onopen = function () {
                    console.log("connection opened");
                };
                ws.onmessage = function (evt) {
                    const data = evt.data;
                    console.log(data)
                };

              
            } 
        }
    </script>
    <title></title>

</head>

<body>
<div id="sse">
    <a href="javascript:myFunction()">Start</a>
</div>

</body>
</html>

Now I am not sure how can I display the data that's pushed every K seconds as a list such that the new data is on the top and the list scrolls automatically?

0 Answers
Related