2017-09-17 18 views
0

clearTimeout(timer)の後に他の機能を再起動するにはどうすればよいですか?clearTimeout(timer)の後に再起動するタイマー

私はrefreshTable()関数を7秒間使用しないと停止します(mousemoveイベントなし)。クライアントが非アクティブの後にマウスを動かすと、リフレッシュを再開できますか?

top関数refreshTable()できるだけそのまま残したいと思います。

Codepen demo

//====DONT EDIT THIS FUNCTION IF POSSIBLE==== 
function refreshTable() { 
    window.clearTimeout(timer); 
    timer = window.setTimeout(refreshTable, 5000); 
    $("body").append("<p>refreshTable every 5 seconds.</p>"); 
} 
//======== 

var timer = window.setTimeout(refreshTable, 0); 




// If theres no activity for 7 seconds do something 
var activityTimeout = setTimeout(clientInActive, 7000); 

function clientResetActive(){ 
    $("body").attr('class', 'active'); 
    clearTimeout(activityTimeout); 
    activityTimeout = setTimeout(clientInActive, 5000); 
    //RESTART TIMER WHILE resetActive 
    //???????? 
} 

// No activity do something. 
function clientInActive(){ 
    $("body").attr('class', 'clientInactive'); 
    $("body").append("<p>clientInActive</p>"); 
    //STOP TIMER WHILE clientInActive 
    clearTimeout(timer); 
} 

// Check for mousemove, could add other events here such as checking for key presses ect. 
$(document).bind('mousemove', function(){clientResetActive()}); 

下の画像のようなものが目標です。

enter image description here

+0

:これを試してみてください! – Eladian

+0

トップ・ファンクション(refreshTable())クライアントが非アクティブのときに停止/一時停止したいと考えています。 クライアントが戻ると、マウスが移動し、refreshTable()が再起動または続行されます。 ありがとう – Kerry7777

+0

"Inactive"は単語なので、私はラクダがそれを助けているかどうかはわかりません。それはIMOを読むのが少し難しくなります。なぜなら、私は「活発なもの」と思うからです。 'clientInactive'はもっと分かりやすい変数名です。 – jdgregson

答えて

1

私はあなたがタイムアウトし、それを止めるの担当の1を開始を担当する一つの機能を有することを示唆していると思います。あなたは、あなたが達成しようとしているものについてもう少し明確にする手助けをしたいが、あなたが必要なものを理解しないでくださいでし

//====DONT EDIT THIS TOP FUNCTION IF POSSIBLE==== 
 
function refreshTable() { 
 
    stopRefreshTable(); 
 
    window.refreshTableTimer = window.setTimeout(refreshTable, 5000); 
 
    $("body").append("<p>refreshTable every 5 seconds.</p>"); 
 
} 
 
//====END==== 
 

 
function startRefreshTable() { 
 
    if(!window.refreshTableTimer) { 
 
     window.refreshTableTimer = window.setTimeout(refreshTable, 0); 
 
    } 
 
} 
 

 
function stopRefreshTable() { 
 
    if(window.refreshTableTimer) { 
 
     self.clearTimeout(window.refreshTableTimer); 
 
    } 
 
    window.refreshTableTimer = null; 
 
} 
 

 
function resetActive(){ 
 
    $("body").attr('class', 'active'); 
 
    clearTimeout(activityTimeout); 
 
    activityTimeout = setTimeout(inActive, 5000); 
 
    //RESTART TIMER WHILE resetActive 
 
    startRefreshTable() 
 
} 
 

 
// No activity do something. 
 
function inActive(){ 
 
    $("body").attr('class', 'inactive'); 
 
    $("body").append("<p>inActive</p>"); 
 
    //STOP TIMER WHILE inActive 
 
    stopRefreshTable(); 
 
} 
 

 
// If theres no activity for 7 seconds do something 
 
var activityTimeout = setTimeout(inActive, 7000); 
 
// Check for mousemove, could add other events here such as checking for key presses ect. 
 
$(document).bind('mousemove', function(){resetActive()}); 
 
startRefreshTable();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

ニース。ありがとうございます! – Kerry7777

+0

1つの小さな変更は、activityTimeout = setTimeout(inActive、7000)を行うことです。 5000より。 – Kerry7777

関連する問題