简体   繁体   English

MouseMove 事件未在 Chrome 中触发

[英]MouseMove Event Not Triggering in Chrome

I have added an event listener for mousemove that triggers a function. For some reason, it is not getting triggered in Chrome.我为触发 function 的mousemove添加了一个事件侦听器。出于某种原因,它没有在 Chrome 中触发。 I can tell because I'm writing to the console during testing.我可以说出来,因为我在测试期间正在写控制台。 The keyup eventlistener and the scroll eventlistener both trigger, but the mousemove does not in Chrome. keyup事件eventlistener器和scroll事件eventlistener器都会触发,但mousemove不会在 Chrome 中触发。 It works fine in Safari and FireFox. Here is my code:它在 Safari 和 FireFox 中工作正常。这是我的代码:

document.body.addEventListener("mousemove", RenewTimeoutTime);
document.body.addEventListener("keyup", RenewTimeoutTime);
document.body.addEventListener("scroll", RenewTimeoutTime);

And the function it triggers:它触发的 function:

function RenewTimeoutTime(){
    var pageName = window.location.href;
    var currentTime = new Date();
    localStorage.setItem("inTimeout", false);
    localStorage.setItem("AI_Timeout_Time", currentTime.getTime() + 270000;
    console.log(localStorage.getItem("AI_Timeout_Time"));
}

It does work, you just have to check if the DOM is loaded first.它确实有效,您只需要检查是否首先加载了 DOM。

Replace the current script with将当前脚本替换为

<script>
document.addEventListener('DOMContentLoaded', addListen, false);  //this is the important bit

function addListen(){
    document.body.addEventListener("keyup", RenewTimeoutTime);
    document.body.addEventListener("scroll", RenewTimeoutTime);
    document.body.addEventListener("mousemove", RenewTimeoutTime);
}

function RenewTimeoutTime(){
    var pageName = window.location.href;
    var currentTime = new Date();
    var time = currentTime.getTime();    //i replaced the time just to be neat
    localStorage.setItem("inTimeout", false);
    localStorage.setItem("AI_Timeout_Time", time + 270000);
    console.log(localStorage.getItem("AI_Timeout_Time"));
}
</script>

Then you should be good to go.那么你应该很高兴去。 Live here在这里

I know this is an old post but I ran into the same issue mentioned and realized what was causing the issue in my case.我知道这是一篇旧帖子,但我遇到了同样的问题,并意识到是什么导致了我的问题。

If you are using Chrome's developer tools and have the Device Toolbar toggled on, the console will not log the mousemove events if the console is open.如果您使用的是 Chrome 的开发人员工具并打开了设备工具栏,则如果控制台处于打开状态,控制台将不会记录 mousemove 事件。 If you close the console while the Device Toolbar is toggled on and move your mouse, the mousemove events will log.如果在设备工具栏打开时关闭控制台并移动鼠标,则会记录 mousemove 事件。

If you toggle the Device Toobar off, you should see the events logging in the console.如果关闭设备工具栏,您应该会在控制台中看到事件记录。

The shortcut for toggling the Device Toolbar on a Windows PC is Control + Shift + M. I imagine it's Command + Shift + M on a Mac but don't quote me.在 Windows PC 上切换设备工具栏的快捷方式是 Control + Shift + M。我想它是 Mac 上的 Command + Shift + M 但不要引用我的话。

enter image description here在此处输入图像描述

The issue seems to be that "mousemove" events are fired infrequently (only on canvas clicks) when the console is open .问题似乎是“mousemove”事件在控制台打开时很少触发(仅在画布点击时)。 If the console is closed, they are fired continuously, when the mouse is moved around the screen.如果控制台关闭,当鼠标在屏幕上移动时,它们会连续触发。

turn off the device toolbar, upper left side of the chrome devtool, ctrl+shift+m关闭设备工具栏,chrome devtool左上角,ctrl+shift+m

Thank you all for your input.感谢大家的投入。 I didn't post the HTML because I didn't think it was necessary.我没有发布 HTML,因为我认为没有必要。 The web app is quite involved so I left it out.网络应用程序非常复杂,所以我把它排除在外。 Long story short, if I watch the console while I move the mouse, the mouse movement doesn't get logged in the console.长话短说,如果我在移动鼠标的同时观看控制台,则鼠标移动不会记录在控制台中。 Clicking will trigger the mousemove event, and the scroll and keyup events do log in the console, but the mousemove does not.单击会触发 mousemove 事件,并且 scroll 和 keyup 事件会记录在控制台中,但 mousemove 不会。 I found out by closing the console, moving my mouse around, and then viewing the console.我是通过关闭控制台,移动鼠标,然后查看控制台来发现的。 Voila!瞧! The mousemove was logged.鼠标移动已被记录。

I changed my code for easier debugging just during testing.我更改了我的代码,以便在测试期间更容易调试。

window.addEventListener("mousemove", function(){console.log("mouse move");});
window.addEventListener("keyup", function(){console.log("keyup");});
window.addEventListener("scroll", function(){console.log("scroll");});

If anyone knows why the console would not log the mousemove while I'm using the developer tools, please let me know.如果有人知道为什么在我使用开发人员工具时控制台不会记录 mousemove,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM