gpt4 book ai didi

javascript - 如何检测 mousemove 何时停止

转载 作者:技术小花猫 更新时间:2023-10-29 12:04:42 24 4
gpt4 key购买 nike

mousemove 完成时,如何使用 eventListener 进行检测?

document.AddEventListener('mousemove', startInteractionTimer, false);

function startInteractionTimer(){
clearInterval(touchInterval);
touchInterval = setInterval(noAction, 6000);
}

我想在 mousemove 结束后立即启动函数 startInteractionTimer 并且我想捕捉它。在上面的代码示例中,如果鼠标移动,它就会启动。

谢谢

编辑: 好吧,我回答了我自己的问题,上面的脚本 --^ 很好。

最佳答案

您始终可以为其创建自定义事件:

(function ($) {
var timeout;
$(document).on('mousemove', function (event) {
if (timeout !== undefined) {
window.clearTimeout(timeout);
}
timeout = window.setTimeout(function () {
// trigger the new event on event.target, so that it can bubble appropriately
$(event.target).trigger('mousemoveend');
}, 100);
});
}(jQuery));

现在你可以这样做:

$('#my-el').on('mousemoveend', function () {
...
});

编辑:

此外,为了与其他 jQuery 事件保持一致:

(function ($) {
$.fn.mousemoveend = function (cb) {
return this.on('mousemoveend', cb);
});
}(jQuery));

现在您可以:

$('#my-el').mousemoveend(fn);

关于javascript - 如何检测 mousemove 何时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15066849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com