最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Weird problem with setTimeout() on Google Chrome - Stack Overflow

programmeradmin1浏览0评论

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.

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.

  • After the box confirm or alert box disappears, which came from the link's onclick, I got '3 seconds passed' box immediately

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

My code is here:

  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 :/

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)
    })
});

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.

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>

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

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.

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.

  • After the box confirm or alert box disappears, which came from the link's onclick, I got '3 seconds passed' box immediately

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

My code is here: http://jsbin./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 :/

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)
    })
});

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.

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>

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

Share Improve this question edited Sep 24, 2010 at 13:15 egon asked Sep 24, 2010 at 12:37 egonegon 811 gold badge1 silver badge4 bronze badges 12
  • 3 "... the idle function works unexpectedly." Unexpectedly how?!?! – T.J. Crowder Commented Sep 24, 2010 at 12:42
  • 3 Wele to StackOverflow. Unlike some other sites, one of the goals is for StackOverflow to stand alone. By all means link to handy live examples as you did, but include the code you're using in your question (that's why Yi Jiang copied it there for you). Link rot can set in easily (external resources get removed, renamed, etc.) and besides, people shouldn't have to go elsewhere if they can see what's wrong from the question itself. Have fun! – T.J. Crowder Commented Sep 24, 2010 at 12:43
  • @T.J. Crowder, +1 for the, basically, HULK SMASH!! =D – David Thomas Commented Sep 24, 2010 at 12:44
  • @David: ...Hulk...smash? – T.J. Crowder Commented Sep 24, 2010 at 12:45
  • 1 I'm getting the same issue in our application; whenever an alert happens, our timers all fire immediately. Seems like a Chrome bug. – Jacob Commented Feb 1, 2011 at 23:32
 |  Show 7 more ments

1 Answer 1

Reset to default 1

If you're getting the box immediately after dismissing the alert or confirmation, that's not odd, that's normal. confirm and alert pletely stop JavaScript execution. The next call to the timer will queue up waiting for the interpreter to bee 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?

发布评论

评论列表(0)

  1. 暂无评论