2016-11-11 3 views
0

앱과 각도가 새롭습니다.앱이 백그라운드로 진행할 때 취소 간격

내 앱은 서버로 get 요청으로 범위를 새로 고치기 위해 간격을 사용합니다. 앱이 백그라운드로 이동할 때 간격을 취소하고 싶습니다.

나는 '$으로 onUnload = 함수()'다른 게시물에보고하지만 좋은 또는 그것을 사용하는 방법을 ... 내가하려고하면 나는 확실하지 않다 : $scope.$onunload=function(){ // Make sure that the interval is destroyed when leaving app $scope.stopRefresh(); };

하지만 그것은 작동하지 않았다. 반면에 탭을 떠나는 stopRefresh() 함수 작업, 그것은 간격을 취소 할 것을

주 ...

, 나는 이리저리 배경을 돌아 오는 간격 whan을 다시 시작할 수있는 방법은 무엇입니까? 여기에 전체 controler를

는 :

angular.module('starter.controllers', []) 

main.controller("temperatureCtrl", ["$scope", "$rootScope", "$window", "$interval", "ArduinoService", function($scope, $rootScope, $window, $interval, service) { 
    $rootScope.arduinoServerUrl = ''; 
    $rootScope.arduinoServerUrl = $window.localStorage['serverUrl']; 

    var autoRefresh; 
    $scope.channels = []; 

    function onTabSelected() { 
$rootScope.arduinoServerUrl = $window.localStorage['serverUrl']; 
} 

$scope.options = { 
loop: false, 
//effect: 'fade', 
speed: 500, 
} 
$scope.data = {}; 
$scope.$watch('data.slider', function(nv, ov) { 
$scope.slider = $scope.data.slider; 
}) 

function startRefresh() { 
autoRefresh = $interval(function() { 
    updateAjax(); 
}, 5000); 
} 


function updateAjax() { 
service.getChannels(function(err, result) { //get json data 
    if (err) { 
    return alert(err); 
    } 
    // puis les mets dans le scope 
    $scope.channels = result.channels; 
    }) 
}; 

$scope.init = function() { //on load page first get data 
updateAjax(); 
//startRefresh() 
} 


$scope.stopRefresh = function() { 
$interval.cancel(autoRefresh); 
}; 

$scope.restartRefresh = function() { 
startRefresh(); 
}; 

$scope.$on('$ionicView.leave', function() { 
// Make sure that the interval is destroyed when leaving tab 
$scope.stopRefresh(); 
}); 

$scope.$onunload=function(){ 
    // Make sure that the interval is destroyed when leaving app 
$scope.stopRefresh(); 
}; 

$scope.$on('$ionicView.enter', function() { 
//star or restart interval scope update 

$scope.restartRefresh(); 
}); 

$scope.$on('$destroy', function() { 
// Make sure that the interval is destroyed too 
$scope.stopRefresh(); 
}); 

}]); 

답변

0

나는 것은 당신이 AngularJS와 '서비스'또는 '공장'처럼 일부 싱글 톤 클래스를 확인해야합니다. 내 코드에서와 마찬가지로 이벤트 재발시 $ inerval을 취소합니다.

factory('BackgroundCheck', function($ionicPlatform){ 
    var service = {}; 
    var inBackground = false; 

    $ionicPlatform.ready(function() {   
     document.addEventListener("resume", function(){inBackground = false;}, false); 
     document.addEventListener("pause", function(){inBackground = true;}, false); 
    }); 

    service.isActive = function(){ 
     return inBackground == false; 
    } 
    return service;  
}) 

나는 당신에게 도움이되기를 바랍니다.

감사 ...

+0

약간의 차이가 있습니다. 나는 각도에 익숙하지 않고 결코 공장을 사용하지 않습니다. ... 무엇이 이루어지는지를 이해하려고 노력합니다. 그러나 당신은 내가하고 싶은 것을 이해합니다. : P. 함수에 $ interval을 주입하고 거기에서 취소 할 수 있습니까? 공장이 어딘가에 전화해야합니까? 나는 진짜 초보자라는 걸 압니다. : p – Nitrof