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

I searched here and found a quick solution to call an action when the user is idle on the page. It basi

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745566732a4633438.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信