【问题标题】:Is it possible to detect idle time even across tabs?即使跨标签也可以检测空闲时间吗?
【发布时间】:2012-04-19 18:55:38
【问题描述】:

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

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

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

【问题讨论】:

标签: javascript detect python-idle


【解决方案1】:

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

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

更新

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

【讨论】:

  • 哈哈,对。这就说得通了。我会考虑其他的技巧,看看我们还能做些什么来产生同样的效果。可能会通知用户每半小时在页面不活动时确认他们处于活动状态,并带有“打盹”功能
【解决方案2】:

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

【讨论】:

  • 如何跨不同的标签?如果用户在打开我们的网站后在 Facebook 上活跃,是否可以检测他们是否活跃?
  • 没有。尝试这样做存在安全问题。您不希望另一个选项卡正在窥探您的银行活动。您只能判断它们是否在您的网站上处于活动状态。如果他们在 Facebook 上,他们就不会活跃。
  • 我正在解决同样的问题,并且正在考虑同样的解决方案.. 但我担心性能瓶颈,因为 localStorage/IndexeDb 都是磁盘操作,所以如果网站被大量使用这些 mousemove 事件肯定会产生影响,因为鼠标移动会持续进行磁盘操作,这在网站中很常见。在此之前我想不出任何其他解决方案。你对此有什么想法吗?
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    代码是:

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

    【讨论】:

    • 您能否扩展您的答案以详细说明您的代码如何帮助解决问题?
    【解决方案5】:

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

         `/*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;     
    }  
    
    `
    

    【讨论】:

      猜你喜欢
      • 2014-04-24
      • 2011-08-22
      • 2012-09-20
      • 2010-10-29
      • 2010-11-13
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 2010-10-14
      相关资源
      最近更新 更多