2015-12-21 40 views
1

你好, 找不到代碼:x有人可以發帖,或者解釋如何在10分鐘的非活動狀態後如何進行彈出操作?在10分鐘不活動後顯示彈出框

當頁面加載後,成員是不活躍的10分零成員會得到一些按鈕的彈出窗口和文本

<div> 
    <p>Away from keyboard?</p> 
    <ul class="button"> 
     <li><a href="#0">I'm Back!</a></li> 
    </ul> 
</div> 
+0

也許嘗試此處提供的解決方案http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly –

回答

1
var seconds = 0; 
     var timeoutCounter = setInterval(function(){ 
     seconds++; 
     if(sec == 600) { 
    // do stuff to launch popup 
    clearInterval(timeoutCounter); 
    } 
     }, 1000); 


    $(document).mousemove(function (e) { 
clearInterval(timeoutCounter); 
     seconds = 0; 
    setInterval(timeoutCounter); 
     }); 
     $(document).keypress(function (e) { 
      clearInterval(timeoutCounter); 
      seconds = 0; 
    setInterval(timeoutCounter); 
     }); 

這基本上運行每一秒 - 如果它是第600秒,它在運行你的代碼後退出。

Source for idle checking

+0

你不如果用戶檢查閒置或不閒。你必須監視鼠標/鍵盤。 – Ilya

+0

@Ilya當用戶進行有意義的交互時,擁有頁面的人應該能夠設置秒= 0。有時你不希望這樣做太泛泛。 – spozun

+0

這不適用於OP在'10分鐘不活動'之後要求的內容,所以我建議你讓OP知道或者用筆記更新答案。 – Franco

0

附上事件到對象documentsetTimeout方法以顯示彈出式沿。做到這一點的方法之一是

var popupTimer, 
     TIME_OUT = 600; 

// function that displays the popup 
function displayPopup() { 
    // display the popup here 
} 

// Set the timeout to display the popup 
popupTimer = setTimeout(displayPopup, TIME_OUT); 

// attch events to the document object 
// you can add more events here based on 
// what events you want to track 
$(document).on('click change keypress', function() { 

    // clear the timeout whenever the event is handled 
    clearTimeout(popupTimer); 

    // Reset the timer 
    popupTimer = setTimeout(displayPopup, TIME_OUT); 
});