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.
$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);
I think you are looking for angular's
$window
wrapper and definingisonfocus
outside the function would help a lot, too.