How do i detect if the user is “idle” with javascript?

别说谁变了你拦得住时间么 提交于 2019-11-29 06:52:48

I think you're searching for this: https://github.com/jasonmcleod/jquery.idle

You may want to listen for some or all of the following events:

mouseMove, mouseClick, mouseUp, mouseDown, keyDown, keyUp, keyPress

set a timer to go off after some duration of idleness (60 seconds?) and that will turn off your switch make sure you check your switch before your ajax requests.

Ideally you'll exponentially throttle your ajax calls to some low value the longer a user remains idle.

$(window).bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', someEvent);
var active = true,
  delay = 60000,
  timer = null;

function someEvent(e)
{
  active = true;
  if (timer) clearTimeout(timer);
  timer = setTimeout(function(t){
    active = false;
  }, delay);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!