3

我有一个脚本,当有新问题发给用户时,它会向用户发送时间敏感的通知。然而,我发现有些人打开他们的电脑去吃午饭,因此错过了通知。

我正在寻找一个脚本来检测用户是否空闲了 5 分钟,如果是这样,它会将它们显示为“离线”并关闭通知。

我很好奇即使跨标签也可以检测到不活动?(例如,如果用户切换到 Facebook.com 的另一个选项卡并在那里保持活跃,即使他们不在我们的网页上,他们也会被视为“活跃”)。

4

5 回答 5

2

当用户不在您身边时发生的所有事情都无法跟踪(幸运的是)。

所以这不是不可能的(考虑安全性)。

更新

现在我想起来了。这可能的,但你不可能做到。如果你的名字是谷歌,你会走得很远,因为很多网站都使用谷歌分析。但除此之外:由于上述原因,不可能。

于 2012-04-19T19:03:48.827 回答
1

当它们处于活动状态时,将它们的最后一个活动存储在数据库表中。您可以使用鼠标移动、按键或其他一些活动来更新时间戳。在用户将看到其在线/离线状态的页面上,使用 ajax 调用定期轮询该表。如果上次活动时间大于 5 分钟,则将其显示为离线或空闲。

于 2012-04-19T19:02:44.380 回答
1

如果我在做这样的事情,我会使用 HTML5 Visibility API 或回退来模糊和聚焦事件,观察用户何时离开页面然后返回......离开意味着取消聚焦浏览器窗口或选项卡(但仍保持页面打开)

但是既然你想对不活动做出反应......嗯,你可以开始一个超时(当然,如果发生诸如提交、单击、更改、鼠标移动等事情,这将需要一个全局事件委托来阻止许多事件)

于 2012-04-19T19:03:36.280 回答
0

代码是:

var inactivityTime = function () {
    var t;
    window.onload = resetTimer;
    document.onmousemove = resetTimer;
    document.onkeypress = resetTimer;

    function logout() {
        alert("You are now logged out.")
        //location.href = 'logout.php'
    }

    function resetTimer() {
        clearTimeout(t);
        t = setTimeout(logout, 3000)
        // 1000 milisec = 1 sec
    }
};
于 2012-07-26T09:53:15.293 回答
0

我想在我的客户网站上实现这个功能。在网络上没有找到任何闲置的解决方案。最后我不得不修改我的代码,想一些逻辑并实现它。代码如下 -

     `/*Put this code inside script tag whereever you want to execute the inactivity popup*/
     var t;
    //set the timeout period  
    var timeoutPeriod = '${inactiveIntervalMillis}';  
    //detect various events  
    callUserEvents();  
     `

//remove the logged Out storage after popup is closed by user  
function removeLocalStorage() {  
localStorage.removeItem("loggedOut");  
}      
//call this function whenever we detect user activity  
function resetUserActivity() {  
resetTimer();  
}  
//If the user is logged out and it clicks on other tabs,the popup will be               displayed there too  
function checkIfUserLoggedOut() {  
    if (localStorage.getItem("loggedOut")) {  
        loadLoginModal("/includes/gadgets/popup-content.jsp", 400, 230,  
            undefined);  
    }  
}  

// Call this method when any window onloads,this helps to check if multiple         tabs are opened by same site  
function incrementCounter() {   
    checkIfUserLoggedOut();  
    if (localStorage.getItem("counter") == "NaN") {  
        localStorage.setItem("counter", "0");  
    } else {  
        var counter = parseInt(localStorage.getItem("counter")) + 1;  
        localStorage.setItem("counter", counter);  
    }  
    resetTimer();  
}  
//after time interval,this method will be called  
function handleIdleTimedOut() {  
//get the current localStorage Object  
    window.sharedCounter = localStorage.getItem("counter");  
//If no tabs are opened,then popup will be shown here.  
    if (window.localCounter == window.sharedCounter) {  
        loadLoginModal("/includes/gadgets/popup-content.jsp", 400,               230,undefined);  
        localStorage.setItem("loggedOut", "true");  
    }   
}  

function resetTimer() {  
//save counterin current Window object,and after timeout period you can     match   it,if by chance multiple tabs were opened,the counter will be     different,popup  wont be shown in current window at incorrect time.  
    window.localCounter = localStorage.getItem("counter");  
    clearTimeout(t);  
    t = setTimeout(handleIdleTimedOut, timeoutPeriod);  
}  
function callUserEvents(){  
window.onload=incrementCounter  
window.onscroll = resetUserActivity;  
window.onmousemove = resetUserActivity;    
window.ondblclick = resetUserActivity;  
window.oncontextmenu = resetUserActivity;  
window.onclick = resetUserActivity;  
window.onkeypress = resetUserActivity;  
window.onpageshow = resetUserActivity;  
window.onresize = resetUserActivity;  
window.onfocus = incrementCounter;  
window.ondrag = resetUserActivity;  
window.oncopy = resetUserActivity;  
window.oncut = resetUserActivity;  
window.onpaste = resetUserActivity;     
}  

`
于 2015-03-04T07:42:46.643 回答