Running function in the background with interval


#1

Is there any way to execute a function (check something and send HTTP POST) when app is in the background or screen is turned off? Moreover I want to set an interval and call it every 5 minutes for example. Is there any way to do it? I know that there is background location plugin.


#2

$scope.onTimeout = function(){

        mytimeout = $timeout($scope.onTimeout,1000);	//Start Timer	
		if ( $scope.counter <= 0 ) $scope.stop(); //Condtion to Stop Timer
}

$scope.stop = function(){
        $timeout.cancel(mytimeout);
}

To start timer just call onTimeout function


#3

Is it Ionic2 syntax? Doesn’t seem like one.


#4

That’s definitely v1 code, which would make sense as you posted this in the v1 category.

I’d look into background fetch if you need to do it while the app is in the background.


#5

My bad, I meant Ionic2


#6

For Ionic 2 you may use following syntax

To start tmer :
var id = setTimeout(function(){

    });
}, 3000);

To Stop timer :
clearTimeout(id);