Are there any javascript/jQuery events that are like an “onmousestop” or any events equivalent to “onmousewheel” for an optical mouse and/or touchpad?

匆匆过客 提交于 2019-12-08 03:28:37

问题


I am trying to create a Javascript-based inactivity timeout for my site. The function which controls inactivity is very simple; a form value recording the "initial time" is reset concomitantly with any event (the assumption being any event should be indicative of activity):

function ResetTimeout() {
    document.forms.TimeoutForm.initialtime.value = (new Date().getTime()) / 1000;
}//This function is called on each event, such as an onclick

In a separate function, when new Date().getTime() (that is, the time at this moment) - document.forms.TimeoutForm.initialtime.value (in milliseconds) > 720000 (12 min), a hidden logout form is submitted. Right now, I am binding these events to the <body> as follows:

<body onclick="ResetTimeout()" onkeydown="ResetTimeout()" onscroll="ResetTimeout()">

This works for almost all cases, since each page on my site requires a vertical scrollbar to see the bottom of the page. Any mouse or touchpad press is also captured, anywhere on screen. However, two valid outliers would be for (a) the user that never scrolls down, and never clicks the mouse/touchpad nor presses any keys but is still somehow engaged on the site and (b) the user that is (unlikely but possible) moving the mouse left/right but never scrolling, clicking, nor pressing any keys. Both of these users would technically be active on the site but would be considered inactive by my code.

I tried accounting for these users by binding the onmouseover or onmousemove events to the <body>, but these events are continuously triggered even if you do not move the cursor at all. I found HTML5's onmousewheel, but this does not work for an optical mouse nor touchpad (function never fires so event is never recorded even as you move the cursor all over the screen).

Is there an event in jQuery or Javascript that is recorded only whenever a user MOVES the mouse anywhere on the screen (and does not remain motionless)? That is, an event which fires only when xcoord2 - xcoord1 (or same for ycoord) does not equal zero over some time interval? My gut feeling is this may be complex and may require the code to record x or y coordinates at different time intervals and then compare. Alternatively, is there any event that would be something like onmousestop (a cursor-in-motion stops moving)? I read about "onwheel" (click here) but I need something for all browsers. Any suggestions? Thanks


回答1:


The mousemove event is not triggered unless the mouse actually moves

The mousemove event is fired when a pointing device (usually a mouse) is moved while over an element

so simply

var timer;

$(document).on('mousemove', function() {
    clearTimeout(timer);

    timer = setTimeout(function() {
        logout();
    }, 720000);
});

FIDDLE



来源:https://stackoverflow.com/questions/24727394/are-there-any-javascript-jquery-events-that-are-like-an-onmousestop-or-any-eve

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