OnmouseMove 无法与 Chome 中的 SetTimeOut 和警报一起使用

发布于 2024-08-27 12:30:09 字数 521 浏览 8 评论 0原文

这段代码有什么问题?它可以在 IE 和 FireFox 中运行,但在 Chrome 中不起作用。 这个想法是函数fnTimeOut将在onmousemove后5秒内被触发(fnTimeOut在文档中附加在onmousemove中)。没关系。但是,当我在 Chrome 中单击“确定”按钮时,fnAlert 功能会立即触发。它应该在我移动鼠标后 5 秒内拍摄......请帮助我。


<input type="button" onclick="alert(1);" value="ok">


<script>

document.onmousemove = fnTimeOut;

var t = null;

function fnAlert()
{
    alert(2);
}

function fnTimeOut()
{
    clearTimeout( t );
    t = setTimeout( fnAlert, 5000 );
}
</script>

What's wrong with this code? It works in IE and FireFox, but Chrome does not work.
The idea is that the function fnTimeOut will be triggered in 5 seconds after onmousemove (fnTimeOut was attach in onmousemove in document). It´s ok. But when, in Chrome, I click on the button "ok" to function fnAlert is triggered instantly. It should be shot just 5 seconds after I move the mouse ... help me please.


<input type="button" onclick="alert(1);" value="ok">


<script>

document.onmousemove = fnTimeOut;

var t = null;

function fnAlert()
{
    alert(2);
}

function fnTimeOut()
{
    clearTimeout( t );
    t = setTimeout( fnAlert, 5000 );
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

属性 2024-09-03 12:30:09

我一定是错过了什么。您的按钮有一个 onclick 表示显示警报框。那里没有代码试图延迟该警报。

我不明白为什么 FF 和 IE 在单击按钮时不会立即显示警报。

如果您希望在鼠标移动或单击按钮后 5 秒调用 fnAlert,则应将按钮上的 onclick 设置为“fnTimeOut()”

I must be missing something. Your button has an onclick that says to show an alert box. There's no code there trying to delay that alert.

I can't figure out how on earth FF and IE would not display the alert immediately when clicking the button.

If what you want is for fnAlert to be called 5 seconds after a mousemove or a click of the button, you should set your onclick on the button to "fnTimeOut()"

鲜肉鲜肉永远不皱 2024-09-03 12:30:09

据我所知,警报和确认语句确实重置了设置超时计时器,这导致在刷新时立即执行给定的代码。

我自己没有尝试,但也许您可以通过空或随机变量语句来确认修改点击内容(当前是警报)?

As far as i read, the alert and confirm statements do reset the settimeout timers, which leads to an immediate execution of given code while flushing.

I didn't try myself, but maybe you could confirm this modifying the content of the click (currently an alert) by a null or random variable statement ?

诗笺 2024-09-03 12:30:09

每次鼠标在文档上移动时 onmousemove 都会被触发,因此警报仅在鼠标停止移动后显示。

话虽如此,它在我的 Chrome 安装上运行良好,这是以下代码。

<html>
<head>
  <script>

  document.onmousemove = fnTimeOut;

  var t = null;

  function fnAlert()
  {
      alert(2);
  }

  function fnTimeOut()
  {
      clearTimeout( t );
      t = setTimeout( fnAlert, 5000 );
  }
  </script>


</head>
<body></body>

</html>

onmousemove gets fired everytime your mouse moves over the document, so your the alert only shows after the mouse stops moving.

Saying that, it worked fine on my Chrome install, this is the following code.

<html>
<head>
  <script>

  document.onmousemove = fnTimeOut;

  var t = null;

  function fnAlert()
  {
      alert(2);
  }

  function fnTimeOut()
  {
      clearTimeout( t );
      t = setTimeout( fnAlert, 5000 );
  }
  </script>


</head>
<body></body>

</html>
鸢与 2024-09-03 12:30:09

同样的问题,我认为这是Chrome浏览器的一个错误......
在 Chrome 8 + Win XP 中;这是错误的;但Chrome 10 + WinXp就可以了;而 chrome 18 又错了……但它不会重新出现在某些操作系统上(例如 Linux……)。有人说这不是浏览器错误,而是操作系统甚至硬件错误。

在我的代码中,我使用“onmouseover”/“onmouseout”来替换“onmousemove”。现在看起来还可以。

但是,在文档元素中 mouseOVER 很困难......

The same issus, I think it's a bug of Chrome browser…
In chrome 8 + Win XP; it's wrong; But Chrome 10 + WinXp is ok; and chrome 18 it's wrong again……but it's NOT reappear on some OS (such as Linux...). Someone say that This is not a browser bug, but an OS or maybe even hardware bug.

In my code, I use "onmouseover"/"onmouseout" to replace "onmousemove". It looks ok now.

BUT, in document element mouseOVER difficult...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文