Jquery 跟踪鼠标挡块
我试图在浏览器中跟踪鼠标移动,如果用户停止鼠标0.5秒,则执行一些代码。我在firebug下面的代码中放置了一个断点,它在var mousestop=function(evt)行中断,但随后跳转到return语句。我错过了一些简单的东西吗?为什么它不执行POST语句?我以类似的方式跟踪鼠标点击,它可以很好地发布到服务器上。只是不要让鼠标停下来Jquery 跟踪鼠标挡块,jquery,Jquery,我试图在浏览器中跟踪鼠标移动,如果用户停止鼠标0.5秒,则执行一些代码。我在firebug下面的代码中放置了一个断点,它在var mousestop=function(evt)行中断,但随后跳转到return语句。我错过了一些简单的东西吗?为什么它不执行POST语句?我以类似的方式跟踪鼠标点击,它可以很好地发布到服务器上。只是不要让鼠标停下来 $.fn.saveStops = function() { $(this).bind('mousemove.clickmap', function(evt
$.fn.saveStops = function() {
$(this).bind('mousemove.clickmap', function(evt) {
var mousestop = function(evt) {
$.post('/heat-save.php', {
x:evt.pageX,
y:evt.pageY,
click:"false",
w:window.innerWidth,
h:window.innerHeight,
l:escape(document.location.pathname)
});
},
thread;
return function() {
clearTimeout(thread);
thread = setTimeout(mousestop, 500);
};
});
};
在执行mousestop时是否可以不定义evt 试着这样做:
return function() {
clearTimeout(thread);
thread = setTimeout(function(){mousestop(evt);}, 500);
};
(new Image).src = "/heat-save.php?x=" + evt.pageX + "&y=" + evt.pageY + "&click=false&w=" + window.innerWidth + "&h=" + window.innerHeight + "&l=" + escape(document.location.pathname;
以下是我将如何重构:
// a closure for good measure...
(function($){
var mousestop = function(evt){
// I would use GET because POST makes 2 round trips
$.get("/heat-save.php", {
"x":evt.pageX,
"y":evt.pageY,
"click":false,
"w":window.innerWidth,
"h":window.innerHeight,
"l":escape(document.location.pathname)
});
},
thread = null;
$.fn.saveStops = function() {
return this.bind("mousemove.clickmap", function(evt) {
clearTimeout(thread);
thread = setTimeout(function(){mousestop(evt);}, 500);
});
};
}(jQuery));
您可以使用以下内容替换ajax调用:
return function() {
clearTimeout(thread);
thread = setTimeout(function(){mousestop(evt);}, 500);
};
(new Image).src = "/heat-save.php?x=" + evt.pageX + "&y=" + evt.pageY + "&click=false&w=" + window.innerWidth + "&h=" + window.innerHeight + "&l=" + escape(document.location.pathname;
此外,确保您的服务器返回“204无内容”状态代码,而不是200,以获得最快速的响应。谢谢您的回复。我更改了返回函数,但它仍然显示相同的行为,并且没有发布。刚刚看到你也在编辑。我要试试这个。好极了!这更有效。我也有(函数($)包装所有点击,但我重构了它,正如你所展示的,所有的工作都很完美!谢谢!(只有14个声誉,否则我也会投票给你:\)不,就这样。我将检查网站的其余部分,但我不相信。尝试制作热图。尽管这样成功了