-2

小さな通知と警告システムがあります。

ウィンドウの状態がぼかしかフォーカスかを検出し、これらのアラートと通知を一覧表示しようとしています。しかし、私の clearInterval メソッドは機能しません。コードは次のとおりです。

$(document).ready(function(){
     setTimeout(function(){loadMessageNotifications()},600);
     setTimeout(function(){loadStoryNotifications()},400);

       var intervalStoryAlerts = setInterval(function(){loadStoryAlerts()},6000);
       var intervalMessageAlerts = setInterval(function(){loadMessageAlerts()},16000); 

    $(window).bind("focus", function(event){

         setTimeout(function(){loadMessageNotifications()},600);
         setTimeout(function(){loadStoryNotifications()},400);

       var intervalStoryAlerts = setInterval(function(){loadStoryAlerts()},6000);
       var intervalMessageAlerts = setInterval(function(){loadMessageAlerts()},16000); 

    }).bind("blur", function(event){

        clearInterval(intervalStoryAlerts);
        clearInterval(intervalMessageAlerts);
    });
});

これらの clearInterval の console.log() 出力は未定義です。

4

2 に答える 2

2

間違った (範囲外の)intervalStoryAlertsとを使用しているintervalMessageAlertsため、新しい間隔を指している可能性があります。代わりに、フォーカスのバインドに再宣言をドロップします。試す:

// variables defined in enclosing scope
var intervalStoryAlerts = ..;
var intervalMessageAlerts = ..;

$(window).bind("focus", function(event){

     setTimeout(function(){loadMessageNotifications()},600);
     setTimeout(function(){loadStoryNotifications()},400);

     // remove var here so that new variables are not created
     intervalStoryAlerts = setInterval(function(){loadStoryAlerts()},6000);
     intervalMessageAlerts = setInterval(function(){loadMessageAlerts()},16000); 
})
于 2013-01-13T21:56:56.127 に答える
1
….bind("focus", function(event){
   // […]
   var intervalStoryAlerts = setInterval(loadStoryAlerts, 6000);
   var intervalMessageAlerts = setInterval(loadMessageAlerts, 16000); 

})

これらのvar宣言により、これら2つの変数が関数に対してローカルになり、コードがレディハンドラーのスコープ内の変数を上書きすることはありません。したがって、値は失われ、間隔はクリアされません。「」は省略してvarください。

于 2013-01-13T22:32:24.650 に答える