How to handle iddle app

Hi all,

I’m trying to handle when my app goes background.

The main purpose is to reload the whole app if the user comes after more than 30seconds with the app in background for example.

I am using $ionicPlatform.on(‘resume’… for that.
It works well when I put no timer. Like the user leaves the app with home button on android, and when he gets back the app just fully reload with the following code:

$ionicPlatform.on(‘resume’,function() {
location.href = location.origin;
});

However, when I try to add a timer to make sure 30 seconds have passed, it no longer fire.
Here is my code, I am using $ionicPlatform.on(‘pause’ …:

var timerOut = false;
	$ionicPlatform.on('pause',function() {
		$timeout(function(){
			timerOut = true;
			alert("timeout happened!");
		},30000)
	});
	
	$ionicPlatform.on('resume',function() {
		if (timerOut) {
			location.href = location.origin;
		}
	});

This is in my app.js, after ionicPlatform.ready

Did anyone manage to handle it?

Thanks

I still need help on that matter.

What I realized so far is that the onpause event actually seems to get fired “onresume”.
Here is the code to prove what I’m saying:

    var timerOut = false;
   document.addEventListener("pause", function () {
		alert("paused");
		$timeout(function(){
			timerOut = true;
			alert("timeout happened!");
		},3000)
	},false);
	document.addEventListener("resume", function () {
		alert("resume");
		setTimeout(function() {
			if (timerOut) {
				location.href = location.origin;
			}
		}, 0);
},false); 

The alert “Paused” doesn’t happen when I close the app, but when I get back to it, right before the alert “resume”.
And that’s why the timeout never starts to run…

Could anyone help me please?
Thanks in advance.