每隔几毫秒在给定的x-y坐标上显示元素
Show Element on given x-y coordinate every few ms
我有一个函数,从我的SignalR
集线器中的另一个客户端接收xy
坐标。每当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.
}
}
为了防止低性能,您可以使用计时器:
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
}
希望能有所帮助。
Js出价
相关文章:
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- 如何在d3.js中返回路径的y坐标
- 在谷歌地图上获取事件的x,y坐标
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 当带有渲染器的DOM元素不在屏幕顶部时,移动了场景的坐标
- 如何在JS Leatflet绘图插件中获取圆的坐标
- 点击(右键点击)使用传单地图库获取图像覆盖的像素坐标
- 查找带有边框的HTML5 Canvas(点击)事件的坐标
- HTML5获取弧的坐标's结束
- 如何在OpenLayers中获取动态绘制的多边形的坐标
- 获取用户位置并将坐标保存在数据库中
- fabric js多边形集合坐标
- 使用D3.js和GeoJson在地图上的某些坐标上设置一个小图像
- 如何创建一个谷歌地图地理坐标数组
- AngularJS-如何在mousemove上存储鼠标坐标
- 使用地理坐标,对更靠近用户的对象进行排序'的位置
- 在坐标上模拟点击坐标js/jQuery
- ChartJS v2:点击坐标的比例值(时间比例)
- 通过鼠标点击获取X和Y坐标(javascript)
- 使用jQuery获取用户坐标