Google Chrome上的setTimeout()奇怪的问题 [英] Weird problem with setTimeout() on Google Chrome

查看:435
本文介绍了Google Chrome上的setTimeout()奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里搜索,找到了一种快速解决方案,可以在用户闲置页面时调用操作.基本上,它可以在所有浏览器上正常工作.

I searched here and found a quick solution to call an action when the user is idle on the page. It basically works well on all browsers.

但是当我在页面上使用警报或确认对话框时,奇怪的问题出现在Google Chrome上.

But when I use an alert or a confirm dialog on the page, the weird problem occurs on Google Chrome.

警报或确认框消失(按确定",取消"或交叉")后,空闲功能会意外起作用.

After the alert or confirm box disappears (Pressed OK, Cancel or Cross), the idle function works unexpectedly.

  • 在该框的确认或警报框消失后(由于链接的onclick而来),我立即获得了"3秒过去"框

在FF,IE和Chrome(最新)上进行了测试.它只是在Chrome上发生.

Tested on FF,IE and Chrome (Latest). It just occurs on Chrome.

我的代码在这里: http://jsbin.com/ifule3

  window.onload = idleTimer;
  function idleTimer() {
    var idleDuration;
    document.onmousemove = idleReset;
    function  idleReset() {
      if (idleDuration) {
        clearTimeout(idleDuration);
        idleDuration = 0;
      }
      idleDuration = setTimeout(function() {
        alert('3 seconds passed.');
      }, 3000)
    }
  };

<a onclick="if(confirm( '?' )) { alert('Ok Pressed.') } else { return false; };">First Link!</a>
<a onclick="alert('test');" >Second Link!</a>

看来我的解释还不够:/

It seems my explanation is not enough :/

我用jQuery更改了代码;

I changed the code with jQuery;

jQuery(document).ready(function() {
    var idleDuration;
    jQuery(document).mousemove(function() {
        if (idleDuration) {
            clearTimeout(idleDuration);
            idleDuration = 0;
        }

        idleDuration = setTimeout(function() {
            someIdleAction();
            window.location = 'some url';
        }, 3000)
    })
});

当我将此代码放到页面上时,它就像一个超级按钮一样工作. 我打开页面,是否进行一些鼠标操作,然后在不移动鼠标的情况下等待3秒钟,得到了空闲警报.这就是我需要的.

When I put this code on my page.It works like a charm. I open the page, make some mouse actions or not, then 3 seconds without moving mouse, I got the idle alert.This is what I need.

当我放置一个仅调用警报框的链接并单击它时,将出现警报框. 然后,我关闭了包装盒,得到了"3秒过去"的空闲警报.

When I put a link that simply calls an alert box and click on it, alert box appears. Then I close the box and I got the idle alert which is '3 seconds passed'.

    <a onclick="if(confirm( 'Are you OK?' )) { alert('Nice.') } else { return false; };">First Link!</a>
    <a onclick="alert('An alert.');" >Second Link!</a>

它仅发生在Google Chrome上.使用IE和FF,一切都很好. 超时增加,没有任何改变.

It just occurs on google chrome. With IE and FF everything is fine. Increasing the timeout, nothing changes.

推荐答案

如果您在撤消警报或确认后立即得到提示框,那并不奇怪,这很正常. confirmalert完全停止JavaScript执行.对计时器的下一次调用将排队等待解释器再次可用,因此消除发出此消息的框并不会让我感到惊讶.这是您看到的唯一行为吗?

If you're getting the box immediately after dismissing the alert or confirmation, that's not odd, that's normal. confirm and alert completely stop JavaScript execution. The next call to the timer will queue up waiting for the interpreter to become available again, so dismissing the box bringing up the message doesn't surprise me. Is that the only behavior you're seeing that's a problem?

这篇关于Google Chrome上的setTimeout()奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆