2012-04-19 13 views
2

私は、新しい質問が送られてきたときに、時間の影響を受けやすい通知をユーザーに送信するスクリプトを用意しています。しかし、私は、一部の人々がコンピュータを開いたままにしておき、昼食を取るため、通知がないことがわかった。タブ間でもアイドル時間を検出できますか?

ユーザーが5分間アイドル状態になっているかどうかを検出するスクリプトをまとめて、「オフライン」と表示し、通知を閉じます。

タブ間でも非アクティブを検出できるかどうか不思議でしたか? (たとえば、ユーザーがFacebook.comへの別のタブに切り替えてそこでアクティブになっている場合は、Webページに具体的にないのに「アクティブ」と表示されます)。

答えて

1

ユーザーがあなたの側にいないときに起きることはすべて追跡できません(幸いにも)。

これはできません(セキュリティについて考えてください)。

UPDATE

今、私はそれについて考えること。 ですが、可能性は非常に低いです。多くのウェブサイトでGoogleアナリティクスが使用されているため、あなたの名前がGoogleだったらあなたは長い道のりを歩んでいました。しかし、それ以外のもの:NOは言及された理由では不可能です。

+0

それは理にかなっている。代替のハックを考えて、同じ効果を生み出すために他に何ができるかを見てみましょう。 「スヌーズ」機能を使用して、30分おきにアクティブであることを確認するようにユーザーに通知する – Donny

1

アクティブなときにデータベーステーブルに最後のアクティビティを格納します。マウスの動き、キー押し、またはその他のアクティビティを使用してタイムスタンプを更新することができます。ユーザーがオンライン/オフライン状態を表示するページでajax呼び出しでその表を定期的にポーリングします。最後のアクティブ時間が5分を超えている場合は、それらをオフラインまたはアイドル状態として示します。

+0

どの程度にわたって異なるタブを行くかのように実装しますか?ユーザーがGoogleのウェブサイトを開いた後にFacebookにアクセスしてアクティブになっている場合、そのユーザーがアクティブであるかどうかを検出できますか? – Donny

+0

いいえ。セキュリティ上の問題があります。あなたは、別のタブがあなたの銀行活動を詮索している可能性を望んでいません。それらがあなたのサイトでアクティブであるかどうかだけを知ることができます。彼らはFacebookにいなければアクティブではないだろう。ハハ、右。 – Buggabill

1

もし私がこのようなことをしているのであれば、私はHTML5 Visibility APIまたはフォールバックを使用して、ユーザーがページを離れてから戻るときにイベントをフォーカスするようにしています。ページを開く)

あなたは非アクティブに反応したいので...タイムアウトを開始することができます(もちろん、送信、クリック、変更などのイベントが発生した場合は停止するために、

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

問題を解決するためにコードがどのように役立つかについて詳しく解説してください。 – forsvarir

0

私は私のクライアントのウェブサイトでこの機能を実装したかったです。私は自分のコードを小枝しなければならなかったweb.Finallyで、このための任意のidleal解決策を見つけるdidntは、いくつかのロジックを考えるとthis.Theコードがbelow--

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

` 
関連する問題