Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idle fired incorrect #51

Open
LimbaHub opened this issue Jul 1, 2019 · 3 comments
Open

Idle fired incorrect #51

LimbaHub opened this issue Jul 1, 2019 · 3 comments

Comments

@LimbaHub
Copy link

LimbaHub commented Jul 1, 2019

After status changed from active to idle once, idle been fired incorrect.
For example, when i keep moving mouse after status changed from idle to active, idle still fired, even througn my mouse never stopped moving.

@bradyep
Copy link

bradyep commented Jan 28, 2020

I'm experiencing the same issue. It correctly detects activity until the first idle event fires, and at that point the idle event will always fire on its interval, regardless of activity on that page.

@pshbot
Copy link

pshbot commented Mar 4, 2020

I came across the same issue in 1.0.6.

I think it's to do with the wakeUp function defined in trackIdleStatus. When it is called in response to mousemove etc after being idle, it calls ifvisible.wakeup(), which fires the wakeup event, which triggers another call to this function. This is happening after the previous timer has been cleared, and before the new timer is set. I think the second call is overwriting the timer token of the first call without clearing it.

I seem to have fixed this by setting the new timer before triggering the wakeup event.

Old code:

trackIdleStatus = function() {
	var timer, wakeUp;
	timer = false;
	wakeUp = function() {
		clearTimeout(timer);
		if (status !== "active") {
			ifvisible.wakeup();
		}
		idleStartedTime = +(new Date());
		return timer = setTimeout(function() {
			if (status === "active") {
				return ifvisible.idle();
			}
		}, idleTime);
	};
	wakeUp();
	addEvent(doc, "mousemove", wakeUp);
	addEvent(doc, "keyup", wakeUp);
	addEvent(doc, "touchstart", wakeUp);
	addEvent(window, "scroll", wakeUp);
	ifvisible.focus(wakeUp);
	return ifvisible.wakeup(wakeUp);
};

New code:

trackIdleStatus = function() {
	var timer, wakeUp;
	timer = false;
	wakeUp = function() {
		clearTimeout(timer);
		timer = setTimeout(function() {
			if (status === "active") {
				return ifvisible.idle();
			}
		}, idleTime);
		if (status !== "active") {
			ifvisible.wakeup();
		}
		idleStartedTime = +(new Date());
		return timer;
	};
	wakeUp();
	addEvent(doc, "mousemove", wakeUp);
	addEvent(doc, "keyup", wakeUp);
	addEvent(doc, "touchstart", wakeUp);
	addEvent(window, "scroll", wakeUp);
	ifvisible.focus(wakeUp);
	return ifvisible.wakeup(wakeUp);
};

@rosskevin
Copy link

I've updated this library and added tests. If you can reproduce the issue I can help get it fixed. See the new test here: https://github.com/rosskevin/ifvisible/blob/master/src/IfVisible.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants