是否有可能在标签之间检测空闲时间?

时间:2012-04-19 18:55:38

标签: javascript detect python-idle

我有一个脚本,当有针对他们的新问题时,会向用户发送时间敏感的通知。但是,我发现有些人将电脑打开并去吃午餐,因此没有通知。

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

我很好奇是否可以检测到标签之间的不活动? (例如,如果用户切换到Facebook.com的另一个标签并在那里保持活跃状态​​,即使它们不在我们的网页上,它们也会被视为“活跃”。

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

如果我在这样的事情上,我使用HTML5可见性API或后退来模糊和聚焦事件观察当用户离开页面然后返回...离开意味着疏忽浏览器窗口或标签(但仍然保持页面打开)

但是因为你想对不活动做出反应...嗯,你可以开始超时(当然,如果发生诸如提交,点击,更改,鼠标移动等事情,那么需要许多事件的全局事件委托来阻止它)

答案 2 :(得分:1)

当用户不在您身边时发生的一切都无法跟踪(幸运的是)。

所以这不可能(想想安全性)。

<强>更新

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

答案 3 :(得分: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
    }
};

答案 4 :(得分: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;     
}  

`