我有一个函数,从xy
集线器中的另一个客户端收到SignalR
坐标。只要clientA
移动鼠标,他的xy-coordinate
就会被发送到ClientB
。
我正在尝试在@
坐标的clientB
屏幕上打印一个简单的x-y
。这是有效的,但唯一的问题是它超级慢(我认为因为每次鼠标移动1px
时都会调用该函数)。当我在clientA
上移动鼠标几秒钟时,clientB
屏幕上打印的“@”就会落后。
这与我编写的代码显示此@
吗?
hub.client.MouseMoved = function (x, y, id) {
id = "@"; //for testing purposes
var e = document.getElementById(id);
if (!e) { //if e is not found, create e
e = $('<div id="' + id + '">' + id + '</div>');
e.css('position', 'absolute');
console.dir(e);
$(e).appendTo(document.body);
}
else {
e = $(e);
}
e.css({ left: x + "px", top: y + "px" }); //set position of cursor to x y coordinate.
}
}
答案 0 :(得分:1)
为防止性能下降,您可以使用计时器:
var timer;
function executeMouseMoved(x, y, id){
id = "@"; //for testing purposes
var e = document.getElementById(id);
if (!e) { //if e is not found, create e
e = $('<div id="' + id + '">' + id + '</div>');
e.css('position', 'absolute');
console.dir(e);
$(e).appendTo(document.body);
}
else {
e = $(e);
}
e.css({ left: x + "px", top: y + "px" }); //set position of cursor to x y coordinate.
}
hub.client.MouseMoved = function (x, y, id) {
clearInterval(timer);
timer = setTimeout(function(){executeMouseMoved(x,y,id);}, 50); //50ms
}
希望它有所帮助。