使用 socket.io 进行音量滑块控制 - 数据太多
我正在尝试借助范围输入字段(jQuery)和socket.io 创建音量控制。
我正在使用此代码来设置所有收听设备上的音量。
socket.on("setVolume", function (data) {
$("#range").val(data);
});
$("#range").change(function() {
socket.emit("setVolume", $("#range").val());
});
现在,当我将范围输入字段拖动一段时间时,我注意到待办事项的队列似乎堆积起来。在保持尽可能高的更新率的同时实现响应式界面的最佳策略是什么?
我尝试这样做:
var timer;
$("#range").change(function() {
window.clearTimeout(timer);
timer = window.setTimeout(function() {
socket.emit("setVolume", $("#range").val());
},10);
});
但这让人感觉非常迟缓。
处理此类大量数据的最佳方法是什么?
I am trying to create a volume control with the help of a range input field (jQuery) and socket.io.
I am using this code to set the volume on all listening devices.
socket.on("setVolume", function (data) {
$("#range").val(data);
});
$("#range").change(function() {
socket.emit("setVolume", $("#range").val());
});
When I now drag the range input field for a while I notice that the queue of things to do seems to pile up. What is the best strategy to achieve a responsive interface while mainting the highest possible update rate?
I tried doing:
var timer;
$("#range").change(function() {
window.clearTimeout(timer);
timer = window.setTimeout(function() {
socket.emit("setVolume", $("#range").val());
},10);
});
But this makes it feel very sluggish.
What is the best way of handling lots of data like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的第二次尝试还不错,但它每次都会重置之前设置的超时 - 这意味着如果更改非常快,您直到最后才会获得更新。像这样的东西怎么样?
我在 JSFiddle 上有一个 示例。
Your second try isn't bad, but it resets the previously set timeout each time--meaning if the change is very fast you won't get an update until toward the end. How about something like this instead?
I have an example up on JSFiddle.
我建议在更改完成时发出
setVolume
事件(类似于 youtube)。例如,当您有音量滑块时,您可以单击它,按住它并更改音量。每次移动鼠标时,change
事件都会在 jQuery 中触发,该事件会按照您所说的方式堆积值。因此,您应该在mouseUp
或类似的东西上执行此操作(jQuery UI 在其slider
中为此提供了一些事件)。我想你明白了。保持原样显然会杀死你的应用程序。
但最重要的是,我认为您应该尝试在客户端执行此操作,而忘记发出任何内容。除非有一个你实际上不能做的原因。
I recommend emiting
setVolume
event when the change is finished (something like youtube). For example when you have slider for volume, you click on it, hold it and change volume. Every time you move mousechange
event is fired in jQuery which piles values as you said. So you should do it onmouseUp
or something like this (jQuery UI provides some events for this in theirslider
). I think you get the idea.Leaving this as it is will obviously kill your app.
But most of all, I think you should try doing this on client-side and forget about emiting anything. Unless there is a reason you actually can't.