views:

131

answers:

1

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

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.

A: 

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?

T.J. Crowder
The problem is that if you click the second link immediately, and close the alert box immediately, a second alert box will appear, containing "3 seconds passed", even if you did all this in less than 3 seconds...(Is this comment understandable ?)
Golmote
@Crowder - Does IE/FFX suppress the setTimeout "thread" when an alert/confirm box shows up, while Chrome just continues the "thread"? (Excuse my lazyness - Don't have time to verify this on my machine.)
DashK
@Golmote: Yes it is. Nothing changes when you increase the timeout. 3 seconds just for test.
egon
@DashK: How they implement it is up to them, provided that while the call to `alert` or `confirm` is happening, **no** other JavaScript related to the page runs at all.
T.J. Crowder
@egon: I get an even weirder result using Chrome for Linux: If I click the first link and press Enter to dismiss the `confirm`, then Enter again to dismiss the `alert`, not only do I immediately get the "3 seconds" alert, but dismissing *that* via the keyboard causes it to show again *instantly*. And this keeps happening as long as I close the alert with the keyboard. If I close the "3 seconds" alert using the mouse instead, that stops the sequence (either immediately, or within a couple of iterations). Does that happen to you?
T.J. Crowder
@T.J: I can't replicate it. Can you tell me what happens when you click the second link then click OK. If you do it with chrome and ff maybe you can see the difference.
egon
@egon: It waits until the three seconds have actually passed.
T.J. Crowder
@T.J: It became weirder now. It doesn't wait on my chrome, even I increase the timeout to 60000. I'll try with chrome for linux.
egon
@T.J: It works great with chrome and ff on linux as you told. But not works with chrome for windows (7).
egon
@egon: Well, the *second* link works great. The first is kind of a problem if closing the alert with the keyboard doesn't work. This is very strange... :-)
T.J. Crowder