<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<body>
<div id="myDiv2" style="width: 600px; height: 650px;"><!-- Plotly chart will be drawn inside this DIV --></div>
</body>
<script>
var socket = io();
socket.on('temp', function(temp){
$('#messages').html(temp);
var layout = {
title: "Live Data Streaming",
xaxis: {
title: "X Axis",
titlefont: {
family: "Verdana, Sans-serif",
size: 18,
color: "#7f7f7f"
}
},
yaxis: {
title: "Live Data",
titlefont: {
family: "Verdana, Sans-serif",
size: 18,
color: "#7f7f7f"
}
}
};
var i=0;
var loop = setInterval(function() {
var trace1 = {
x: [i],
y: [temp],
type: 'line'
};
i++;
}, 5000);
var data = [trace1];
Plotly.newPlot('myDiv2', data, layout);
});
</script>
套接字正在偵聽變量temp。我希望每五秒鐘將這個臨時變量生成一個圖表。現在,我正在積極使用並每五秒設置一次間隔,但圖形不會顯示在我的html中。如果我刪除setInterval,圖表顯示正常,但舊的臨時替換爲新的臨時而不是繼續圖形。任何想法如何在Java腳本中直播這些數據。在javascript中實時流式傳輸