如何使用 javascript 计时来控制鼠标停止和鼠标移动事件
所以我在 aspx 页面上有一个控件(地图)。 我想编写一些 javascript 来加载以下设置:
当鼠标停止在控件上时 = 一些代码
当鼠标移动时 = 一些代码(但前提是移动长于 2.5 亿秒)
这可以在停止时触发代码,然后在移动时触发代码...
function setupmousemovement() {
var map1 = document.getElementById('Map_Panel');
var map = document.getElementById('Map1');
map1.onmousemove = (function() {
var onmousestop = function() {
//code to do on stop
}, thread;
return function() {
//code to do on mouse move
clearTimeout(thread);
thread = setTimeout(onmousestop, 25);
};
})();
};
但我不知道如何在移动代码中引入延迟。 我以为我已经拥有了它......
function setupmousemovement() {
var map1 = document.getElementById('Map_Panel');
var map = document.getElementById('Map1');
map1.onmousemove = (function() {
var onmousestop = function() {
//code to do on stop
clearTimeout(thread2);
}, thread;
return function() {
thread2 = setTimeout("code to do on mouse move", 250);
clearTimeout(thread);
thread = setTimeout(onmousestop, 25);
};
})();
};
但它的行为并不像我想象的那样。 移动中的“thread2”永远不会被停止清除。 我缺少什么?
So I have a control (a map) on an aspx page. I want to write some javascript to onload setup the following:
when mouse stops on control = some code
when mouse moves = some code (but only if the move is longer than 250 mil sec)
This works to trigger code on stop and then on move...
function setupmousemovement() {
var map1 = document.getElementById('Map_Panel');
var map = document.getElementById('Map1');
map1.onmousemove = (function() {
var onmousestop = function() {
//code to do on stop
}, thread;
return function() {
//code to do on mouse move
clearTimeout(thread);
thread = setTimeout(onmousestop, 25);
};
})();
};
But I cannot figure out how to introduce a delay into the on move code. I thought I had it with this...
function setupmousemovement() {
var map1 = document.getElementById('Map_Panel');
var map = document.getElementById('Map1');
map1.onmousemove = (function() {
var onmousestop = function() {
//code to do on stop
clearTimeout(thread2);
}, thread;
return function() {
thread2 = setTimeout("code to do on mouse move", 250);
clearTimeout(thread);
thread = setTimeout(onmousestop, 25);
};
})();
};
But it does not behave as I thought it would. The on move "thread2" is never cleared by the stop. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个棘手的问题。 一些修补导致了这一点:
您的代码不起作用的原因是 mousemove 在鼠标移动时重复触发,并且您每次都开始新的超时。
That is a tricky one. A little bit of tinkering resulted in this:
The reason your code does not work is that mousemove fires repeatedly while the mouse is moving and you are starting new timeouts every time.