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

后端 未结 4 1394
忘掉有多难
忘掉有多难 2020-12-17 23:05

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

相关标签:
4条回答
  • 2020-12-17 23:25

    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

    0 讨论(0)
  • 2020-12-17 23:32

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

    0 讨论(0)
  • 2020-12-17 23:36

    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);
    }
    
    0 讨论(0)
  • 2020-12-17 23:39

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

    0 讨论(0)
提交回复
热议问题