2016-07-25 4 views
0

を働いていないnullに変数を設定(onBlur()onFocus()を参照)関数は、まだcheckForChangesSupport()を参照してください(同じのsetIntervalでgetTableSupport.phpをロードしています。機能onBlurイベントが設定されている場合は、すべての5000ms私はonFocusが再び呼び出されるまで、いずれかの方法は、supportchatintervalがちょうど整数である。てclearIntervalと下の私のコードで<code>supportchatinterval = null;</code>と<code>clearInterval(supportchatinterval);</code>を持つにもかかわらず

<script> 

supportchatinterval = 5000; 

$(document).ready(function(){ 
    checkForChangesSupport(); 
    setTimeout(checkForChangesSupport, supportchatinterval); 
}); 

function checkForChangesSupport() { 
    $.get("getCountSupport.php", function(response) { 
     if(response == 1) { 
     refreshTableSupport(); 
     } 
setTimeout(checkForChangesSupport, supportchatinterval) 
    }); 
} 

function refreshTableSupport(){ 
    $('#tableHolderSupport').load('getTableSupport.php'); 
} 

</script> 

<script type="text/javascript"> 

function onBlur(){ 
document.body.className = 'blurred'; 
$.get("afk.php?afk=1"); 
supportchatinterval = null; 
clearInterval(supportchatinterval); 
}; 

function onFocus() { 
document.body.className = 'focused'; 
$.get("afk.php?afk=0"); 
    supportchatinterval = 5000; 
refreshTableSupport(); 
} 

</script> 
+0

1)あなたがしています'setTimeout'と' setInterval'を混在させます。 2) 'setTimeout'はタイムアウトIDを返します。 – gcampbell

+0

ありがとう私は明らかに盲目です – michelle

+0

PHPはこの質問とは関係ありません、タグを削除してください:) –

答えて

1

新しいタイムアウトがたびcheckForChangesSupport()ランを作成しているので、クリアする一切の間隔は、ありません。のsetIntervalを停止することができず、 "クリアする"赤"。

これを防ぐには、フラグを導入して、関数を実行するかどうかをチェックします。また、checkForChangesSupport()に電話して、タイマーをもう一度開始する必要があります。

<script> 

supportchatinterval = 5000; 
var flag = 1; 

$(document).ready(function(){ 
    checkForChangesSupport(); 
    setTimeout(checkForChangesSupport, supportchatinterval); 
}); 

function checkForChangesSupport() { 
    if(flag){ 
     $.get("getCountSupport.php", function(response) { 
     if(response == 1) { 
      refreshTableSupport(); 
     } 
     setTimeout(checkForChangesSupport, supportchatinterval) 
     }); 
    } 
} 

function refreshTableSupport(){ 
    $('#tableHolderSupport').load('getTableSupport.php'); 
} 

</script> 

<script type="text/javascript"> 

function onBlur(){ 
document.body.className = 'blurred'; 
$.get("afk.php?afk=1"); 
flag = 0; 
}; 

function onFocus() { 
document.body.className = 'focused'; 
$.get("afk.php?afk=0"); 
flag = 1; 
checkForChangesSupport(); 
} 

</script> 
+0

これは恥ずかしいことですが、私はこれを打ち明けて、clearIntervalに関するドキュメントを読ませてください。すべてがうまくいくなら、私はそれを受け入れるでしょう。ありがとうございました。ごめんなさい。 – michelle

+0

誰かが上記の関数でsetTimeoutを使用していると指摘しましたが、setIntervalに変更する必要がありますか? – michelle

+0

最後の2つのステートメントをうまくフリップします。それ以外の場合は、 'clearInterval(null);' – aquinas

1

supportchatintervalあなたのケースでは、現在実行中のタイムアウトとは関係ありません。コードが実行されるまでの時間はすべてです。あなたがする必要があるのは、生成された実際のタイムアウトIDを保存することです。

theTimeout = setTimeout(checkForChangesSupport, supportchatinterval); 

、あなたはその

window.clearTimeout(theTimeout); 
0

を使用し、私はあなたのユースケースに、より適切である、setIntervalを使用することをお勧め。

次に、ぼかしでクリアできます。

あなたの主な問題は、それが設定されているときに返され、それをキャンセルするために引数OS clearIntervalとして使用されている区間(引数として与えられた)とそのハンドル遅延間の混乱です。

コード:

// This is a delay between function calls, in milliseconds 
    supportchatinterval = 5000; 
    // This will be the handle returned by setInterval, which be usefull to cancel 
    // it later 
    intervalid = null; 

    $(document).ready(function(){ 
     checkForChangesSupport(); 
     intervalid = setInterval(checkForChangesSupport, supportchatinterval); 
    }); 

    function checkForChangesSupport() { 
     $.get("getCountSupport.php", function(response) { 
      if (response == 1) { 
       refreshTableSupport(); 
      } 
     }); 
    } 

    function refreshTableSupport(){ 
     $('#tableHolderSupport').load('getTableSupport.php'); 
    } 

    function onBlur(){ 
     document.body.className = 'blurred'; 
     $.get("afk.php?afk=1"); 
     clearInterval(intervalid); 
    }; 

    function onFocus() { 
     document.body.className = 'focused'; 
     $.get("afk.php?afk=0"); 
     refreshTableSupport(); 
    } 

あなたはぼかしのインターバル機能を呼び出しを停止したいのですが、結果が存在する場合にも、ちょうどそこclearIntervalを呼び出さない場合にのみ:

function checkForChangesSupport() { 
     $.get("getCountSupport.php", function(response) { 
      if (response == 1) { 
       refreshTableSupport(); 
       clearInterval(intervalid); 
      } 
     }); 
    } 
関連する問題

 関連する問題