angularjs - angular.js run code ONLY if window is in focus -
i have setinterval function runs every 5 seconds. want code inside function run if window infocus. i've tried doesnt seem 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 looking angular's $window
wrapper , defining isonfocus
outside function lot, too.
app.controller('ctrl', function($scope, $window) { var hasfocus; $scope.myfunction = function() { $window.onblur = function() { console.log('>onblur'); hasfocus = false; }; $window.onfocus = function() { console.log('>onfocus'); hasfocus = true; }; console.log(hasfocus ? 'focused' : 'not focused'); }; setinterval(function() { // or $interval $scope.myfunction(); }, 5000); });
Comments
Post a Comment