Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

javascript - How to display last 10 updates from websocket in div?

I have a web socket streaming data to a webpage and I only want to display the last 10 updates. If i do .innerHTML = result; then the entry gets overridden on every new update and if i do .innerHTML += result; then the incoming messages get appended to the div which ends up crashing the webpage due to the amount of data coming in. Does anyone have any ideas how i might go about achieving this?

Thanks

question from:https://stackoverflow.com/questions/65908234/how-to-display-last-10-updates-from-websocket-in-div

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

keep an array, each time data is emitted from the socket add the items to the array. When it reaches length of 10 on each emit from the socket use unshift on the array with the new value and pop to remove the last, then, set innerHTML = '' (empty string) and iterate on the array and add the items to innerHTML using +=


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...