2017-04-21 5 views
0

ブラウザで現在のタブが開いているときのみ、2秒ごとに呼び出される角度js関数があります。現在のページがブラウザでアクティブかどうかを確認する方法はありますか?Angular関数を実行するのは、現在のタブがブラウザでアクティブな場合のみです

$scope.callAtInterval = function() { 

     var url = "http://127.0.0.1/test"; 

     $http.get(url).success(function(response) { 
      $scope.initial = response; 

     }, 
     function errorCallback(response) { 
      $scope.result=response; 
         }); 
} 

$interval(function(){ $scope.callAtInterval(); }, 5000); 

}

+0

あなたのコードを投稿! –

+0

@ sachila ranawaka私はコードを掲載しました。ここで5秒ごとにapi呼び出しが行われ、データが更新されます。しかし、私は現在のタブがブラウザでアクティブになっているときにのみしたいです。 –

+0

htmlコードも表示 –

答えて

0

あなたは、ウィンドウのフォーカスとブラーのイベントを使用します。

$(window).on("blur focus", function(e) { 
    var prevType = $(this).data("prevType"); 

    if (prevType != e.type) { // reduce double fire issues 
     switch (e.type) { 
      case "blur": 
       // cancel your interval function 
       break; 
      case "focus": 
       // emit your interval function 
       break; 
     } 
    } 

    $(this).data("prevType", e.type); 
}) 
関連する問題