title: angular.js run code ONLY if window is in focus
body: <p>I have a setInterval function that runs every 5 seconds.  I want the code inside that function to only run if the window is infocus.  I've tried but it doesnt seem to work.</p>
  
  <pre><code>$scope.myFunction = function() {
    var isonfocus = true;  
  
    window.onblur = function() {  
      isonfocus = false;  
    }  
  
    window.onfocus = function() {  
      isonfocus = true;  
    }
  
    if (isonfocus) {   
      console.log("focused!");
    }
  }
  
  setInterval(function() {
    $scope.myFunction();
  }, 5000);
  </code></pre>