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

南笙酒味 提交于 2019-11-28 00:15:35

问题


I'm developing a "real-time" web application which sends AJAX requests to the server every 10 seconds. Obviously this is very bandwidth-intensive and I would like to know if there's any solution to this.

My idea is checking if the user doesn't move his mouse for X seconds. How can I accomplish this?


回答1:


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




回答2:


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



回答3:


http://docs.jquery.com/Tutorials:Mouse_Position Hope that helps!




回答4:


Here's another way: http://www.tek-tips.com/faqs.cfm?fid=2700
And using Jquery : http://www.bedroomlan.org/coding/detecting-%E2%80%98idle%E2%80%99-and-%E2%80%98away%E2%80%99-timeouts-javascript



来源:https://stackoverflow.com/questions/4102185/how-do-i-detect-if-the-user-is-idle-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!